Skip to content

Commit 6f19c97

Browse files
committed
fix(repo): codegen checks
1 parent 883287b commit 6f19c97

File tree

7 files changed

+151
-1
lines changed

7 files changed

+151
-1
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
# Check if postgrest-js common types were modified
5+
if git diff --cached --name-only | grep -q "packages/core/postgrest-js/src/types/common/"; then
6+
echo "📝 Detected changes in postgrest-js common types..."
7+
echo "🔄 Running codegen to sync types..."
8+
9+
npm run codegen
10+
11+
# Add the generated files to the commit
12+
git add packages/core/supabase-js/src/lib/rest/types/common/
13+
14+
echo "✅ Generated types have been updated and staged"
15+
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 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/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/supabase-js/src/lib/rest/types/common/common.ts

Lines changed: 11 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
@@ -54,3 +64,4 @@ export type GenericSchema = {
5464
export type ClientServerOptions = {
5565
PostgrestVersion?: string
5666
}
67+

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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
const crypto = require('crypto')
17+
18+
const SOURCE_DIR = path.join(__dirname, '../packages/core/postgrest-js/src/types/common')
19+
const DEST_DIR = path.join(__dirname, '../packages/core/supabase-js/src/lib/rest/types/common')
20+
21+
const HEADER_COMMENT = `/**
22+
* AUTO-GENERATED FILE - DO NOT EDIT
23+
*
24+
* This file is automatically synchronized from @supabase/postgrest-js
25+
* Source: packages/core/postgrest-js/src/types/common/
26+
*
27+
* To update this file, modify the source in postgrest-js and run:
28+
* npm run codegen
29+
*/
30+
31+
`
32+
33+
function getFileHash(filePath) {
34+
if (!fs.existsSync(filePath)) return null
35+
const content = fs.readFileSync(filePath, 'utf8')
36+
return crypto.createHash('md5').update(content).digest('hex')
37+
}
38+
39+
function stripGeneratedHeader(content) {
40+
// Remove existing AUTO-GENERATED header if present
41+
const headerRegex = /^\/\*\*\s*\n\s*\*\s*AUTO-GENERATED FILE[\s\S]*?\*\/\s*\n\s*\n/
42+
return content.replace(headerRegex, '')
43+
}
44+
45+
function syncFile(fileName) {
46+
const sourcePath = path.join(SOURCE_DIR, fileName)
47+
const destPath = path.join(DEST_DIR, fileName)
48+
49+
if (!fs.existsSync(sourcePath)) {
50+
console.error(`❌ Source file not found: ${sourcePath}`)
51+
process.exit(1)
52+
}
53+
54+
const sourceContent = fs.readFileSync(sourcePath, 'utf8')
55+
const sourceHash = crypto.createHash('md5').update(sourceContent).digest('hex')
56+
57+
// Check if destination exists and compare content (excluding header)
58+
let needsUpdate = true
59+
if (fs.existsSync(destPath)) {
60+
const destContent = fs.readFileSync(destPath, 'utf8')
61+
const destContentStripped = stripGeneratedHeader(destContent)
62+
const destHash = crypto.createHash('md5').update(destContentStripped).digest('hex')
63+
64+
if (sourceHash === destHash) {
65+
needsUpdate = false
66+
}
67+
}
68+
69+
if (needsUpdate) {
70+
const contentWithHeader = HEADER_COMMENT + sourceContent
71+
fs.writeFileSync(destPath, contentWithHeader, 'utf8')
72+
console.log(`✅ Synced: ${fileName}`)
73+
return true
74+
} else {
75+
console.log(`⏭️ Unchanged: ${fileName}`)
76+
return false
77+
}
78+
}
79+
80+
function main() {
81+
console.log('🔄 Syncing common types from postgrest-js to supabase-js...\n')
82+
83+
// Ensure destination directory exists
84+
if (!fs.existsSync(DEST_DIR)) {
85+
fs.mkdirSync(DEST_DIR, { recursive: true })
86+
console.log(`📁 Created directory: ${DEST_DIR}\n`)
87+
}
88+
89+
// Get all .ts files from source directory
90+
const sourceFiles = fs.readdirSync(SOURCE_DIR).filter(file => file.endsWith('.ts'))
91+
92+
if (sourceFiles.length === 0) {
93+
console.error(`❌ No TypeScript files found in ${SOURCE_DIR}`)
94+
process.exit(1)
95+
}
96+
97+
let changedCount = 0
98+
for (const file of sourceFiles) {
99+
const changed = syncFile(file)
100+
if (changed) changedCount++
101+
}
102+
103+
console.log(`\n✨ Sync complete: ${changedCount} file(s) updated, ${sourceFiles.length - changedCount} unchanged`)
104+
105+
if (changedCount > 0) {
106+
console.log('\n⚠️ Generated files were updated. Please commit these changes.')
107+
}
108+
}
109+
110+
main()

0 commit comments

Comments
 (0)