Skip to content

Commit 698c306

Browse files
authored
fix(repo): convert postbuild to explicit codegen (#1778)
1 parent c4554ff commit 698c306

File tree

10 files changed

+140
-7
lines changed

10 files changed

+140
-7
lines changed

.github/workflows/ci-core.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ jobs:
4444
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
4545
# - run: npx nx-cloud record -- echo Hello World
4646
# When you enable task distribution, run the e2e-ci task instead of e2e
47+
- name: Check generated types are in sync
48+
run: npm run codegen:check
49+
4750
- name: Build affected packages
4851
run: npx nx affected --target=build
4952

.husky/pre-commit

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Check if postgrest-js common types were modified
2+
if git diff --cached --name-only | grep -q "packages/core/postgrest-js/src/types/common/"; then
3+
echo "📝 Detected changes in postgrest-js common types..."
4+
echo "🔄 Running codegen to sync types..."
5+
6+
npm run codegen
7+
8+
# Add the generated files to the commit
9+
git add packages/core/supabase-js/src/lib/rest/types/common/
10+
11+
echo "✅ Generated types have been updated and staged"
12+
fi

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"scripts": {
66
"commit": "cz",
77
"prepare": "husky",
8+
"codegen": "node scripts/sync-common-types.js",
9+
"codegen:check": "node scripts/sync-common-types.js && git diff --exit-code packages/core/supabase-js/src/lib/rest/types/common && echo '\n✅ Generated types are in sync!' || (echo '\n❌ Generated types are out of sync.\n Run: npm run codegen\n Then commit the changes.\n' && exit 1)",
810
"release-canary": "npx tsx scripts/release-canary.ts",
911
"release-stable": "npx tsx scripts/release-stable.ts"
1012
},

packages/core/auth-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"clean": "rimraf dist docs",
3030
"coverage": "echo \"run npm test\"",
3131
"build:node18": "npm run clean && npm run build:main && npm run build:module",
32-
"build": "npm run clean && npx nx build:main auth-js && npx nx build:module auth-js",
32+
"build": "npm run clean && npm run build:main && npm run build:module",
3333
"build:main": "tsc -p tsconfig.json",
3434
"build:module": "tsc -p tsconfig.module.json",
3535
"test:auth": "npm run test:clean && npm run test:infra && npm run test:suite && npm run test:clean",

packages/core/postgrest-js/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"format": "node scripts/format.js",
3838
"format:check": "node scripts/format.js check",
3939
"build": "npm run clean && npm run build:cjs && npm run build:esm",
40-
"postbuild": "cp -rf ./src/types/common ../supabase-js/src/lib/rest/types/common",
4140
"build:cjs": "tsc -p tsconfig.json",
4241
"build:esm": "cpy wrapper.mjs dist/esm/",
4342
"docs": "typedoc src/index.ts --out docs/v2",

packages/core/storage-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"scripts": {
2828
"clean": "rimraf dist docs/v2",
29-
"build": "npx nx clean storage-js && npx nx build:main storage-js && npx nx build:module storage-js && npx nx build:umd storage-js",
29+
"build": "npm run clean && npm run build:main && npm run build:module && npm run build:umd",
3030
"build:main": "tsc -p tsconfig.json",
3131
"build:module": "tsc -p tsconfig.module.json",
3232
"build:umd": "webpack",

packages/core/supabase-js/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
},
2727
"scripts": {
2828
"clean": "rimraf dist docs/v2",
29-
"build": "npm run clean && npx nx build:main supabase-js && npx nx build:module supabase-js && npx nx build:umd supabase-js",
29+
"build": "npm run clean && npm run build:main && npm run build:module && npm run build:umd",
3030
"build:main": "tsc -p tsconfig.json",
3131
"build:module": "tsc -p tsconfig.module.json",
3232
"build:umd": "webpack --env mode=production",
33-
"test": "npx nx test:types supabase-js && npx nx test:run supabase-js",
34-
"test:all": "npx nx test:types supabase-js && npx nx test:run supabase-js && npx nx test:integration supabase-js && npx nx test:integration:browser supabase-js",
33+
"test": "npm run test:types && npm run test:run",
34+
"test:all": "npm run test:types && npm run test:run && npm run test:integration && npm run test:integration:browser",
3535
"test:run": "jest --runInBand --detectOpenHandles",
3636
"test:unit": "jest --runInBand --detectOpenHandles test/unit",
3737
"test:coverage": "jest --runInBand --coverage --testPathIgnorePatterns=\"test/integration|test/deno\"",
@@ -41,7 +41,7 @@
4141
"test:watch": "jest --watch --verbose false --silent false",
4242
"test:node:playwright": "cd test/integration/node-browser && npm install && cp ../../../dist/umd/supabase.js . && npm run test",
4343
"test:bun": "cd test/integration/bun && bun install && bun test",
44-
"test:types": "npx nx build:module supabase-js && tsd --files test/types/*.test-d.ts && jsr publish --dry-run --allow-dirty",
44+
"test:types": "npm run build:module && tsd --files test/types/*.test-d.ts && jsr publish --dry-run --allow-dirty",
4545
"docs": "typedoc --entryPoints src/index.ts --out docs/v2",
4646
"docs:json": "typedoc --entryPoints src/index.ts --json docs/v2/spec.json --excludeExternals",
4747
"serve:coverage": "npx nx test:coverage supabase-js && serve test/coverage",

packages/core/supabase-js/src/lib/rest/types/common/common.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* AUTO-GENERATED FILE - DO NOT EDIT
3+
*
4+
* This file is automatically synchronized from @supabase/postgrest-js
5+
* Source: packages/core/postgrest-js/src/types/common/
6+
*
7+
* To update this file, modify the source in postgrest-js and run:
8+
* npm run codegen
9+
*/
10+
111
// Types that are shared between supabase-js and postgrest-js
212

313
export type Fetch = typeof fetch

packages/core/supabase-js/src/lib/rest/types/common/rpc.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* AUTO-GENERATED FILE - DO NOT EDIT
3+
*
4+
* This file is automatically synchronized from @supabase/postgrest-js
5+
* Source: packages/core/postgrest-js/src/types/common/
6+
*
7+
* To update this file, modify the source in postgrest-js and run:
8+
* npm run codegen
9+
*/
10+
111
import type { GenericFunction, GenericSchema, GenericSetofOption } from './common'
212

313
// Functions matching utils

scripts/sync-common-types.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Sync common type definitions from postgrest-js to supabase-js
5+
*
6+
* This script copies shared type definitions from @supabase/postgrest-js
7+
* to @supabase/supabase-js to ensure type compatibility across complex
8+
* generic types and conditional types.
9+
*
10+
* Source of truth: packages/core/postgrest-js/src/types/common/
11+
* Destination: packages/core/supabase-js/src/lib/rest/types/common/
12+
*/
13+
14+
const fs = require('fs')
15+
const path = require('path')
16+
17+
const SOURCE_DIR = path.join(__dirname, '../packages/core/postgrest-js/src/types/common')
18+
const DEST_DIR = path.join(__dirname, '../packages/core/supabase-js/src/lib/rest/types/common')
19+
20+
const HEADER_COMMENT = `/**
21+
* AUTO-GENERATED FILE - DO NOT EDIT
22+
*
23+
* This file is automatically synchronized from @supabase/postgrest-js
24+
* Source: packages/core/postgrest-js/src/types/common/
25+
*
26+
* To update this file, modify the source in postgrest-js and run:
27+
* npm run codegen
28+
*/
29+
30+
`
31+
32+
function syncFile(fileName) {
33+
const sourcePath = path.join(SOURCE_DIR, fileName)
34+
const destPath = path.join(DEST_DIR, fileName)
35+
36+
if (!fs.existsSync(sourcePath)) {
37+
console.error(`❌ Source file not found: ${sourcePath}`)
38+
process.exit(1)
39+
}
40+
41+
const sourceContent = fs.readFileSync(sourcePath, 'utf8')
42+
const expectedContent = HEADER_COMMENT + sourceContent
43+
44+
// Check if destination exists and compare with what we would generate
45+
let needsUpdate = true
46+
if (fs.existsSync(destPath)) {
47+
const destContent = fs.readFileSync(destPath, 'utf8')
48+
49+
// Compare the full expected output with the current file
50+
if (destContent === expectedContent) {
51+
needsUpdate = false
52+
}
53+
}
54+
55+
if (needsUpdate) {
56+
fs.writeFileSync(destPath, expectedContent, 'utf8')
57+
console.log(`✅ Synced: ${fileName}`)
58+
return true
59+
} else {
60+
console.log(`⏭️ Unchanged: ${fileName}`)
61+
return false
62+
}
63+
}
64+
65+
function main() {
66+
console.log('🔄 Syncing common types from postgrest-js to supabase-js...\n')
67+
68+
// Ensure destination directory exists
69+
if (!fs.existsSync(DEST_DIR)) {
70+
fs.mkdirSync(DEST_DIR, { recursive: true })
71+
console.log(`📁 Created directory: ${DEST_DIR}\n`)
72+
}
73+
74+
// Get all .ts files from source directory
75+
const sourceFiles = fs.readdirSync(SOURCE_DIR).filter((file) => file.endsWith('.ts'))
76+
77+
if (sourceFiles.length === 0) {
78+
console.error(`❌ No TypeScript files found in ${SOURCE_DIR}`)
79+
process.exit(1)
80+
}
81+
82+
let changedCount = 0
83+
for (const file of sourceFiles) {
84+
const changed = syncFile(file)
85+
if (changed) changedCount++
86+
}
87+
88+
console.log(
89+
`\n✨ Sync complete: ${changedCount} file(s) updated, ${sourceFiles.length - changedCount} unchanged`
90+
)
91+
92+
if (changedCount > 0) {
93+
console.log('\n⚠️ Generated files were updated. Please commit these changes.')
94+
}
95+
}
96+
97+
main()

0 commit comments

Comments
 (0)