diff --git a/.github/workflows/ci-core.yml b/.github/workflows/ci-core.yml index 276c54015..0c995cc22 100644 --- a/.github/workflows/ci-core.yml +++ b/.github/workflows/ci-core.yml @@ -44,6 +44,9 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: npx nx-cloud record -- echo Hello World # When you enable task distribution, run the e2e-ci task instead of e2e + - name: Check generated types are in sync + run: npm run codegen:check + - name: Build affected packages run: npx nx affected --target=build diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 000000000..508cc4dce --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,12 @@ +# Check if postgrest-js common types were modified +if git diff --cached --name-only | grep -q "packages/core/postgrest-js/src/types/common/"; then + echo "šŸ“ Detected changes in postgrest-js common types..." + echo "šŸ”„ Running codegen to sync types..." + + npm run codegen + + # Add the generated files to the commit + git add packages/core/supabase-js/src/lib/rest/types/common/ + + echo "āœ… Generated types have been updated and staged" +fi diff --git a/package.json b/package.json index dd3b3ede9..a56b49e71 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "scripts": { "commit": "cz", "prepare": "husky", + "codegen": "node scripts/sync-common-types.js", + "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)", "release-canary": "npx tsx scripts/release-canary.ts", "release-stable": "npx tsx scripts/release-stable.ts" }, diff --git a/packages/core/auth-js/package.json b/packages/core/auth-js/package.json index f7cad7d88..7ebf21c3e 100644 --- a/packages/core/auth-js/package.json +++ b/packages/core/auth-js/package.json @@ -29,7 +29,7 @@ "clean": "rimraf dist docs", "coverage": "echo \"run npm test\"", "build:node18": "npm run clean && npm run build:main && npm run build:module", - "build": "npm run clean && npx nx build:main auth-js && npx nx build:module auth-js", + "build": "npm run clean && npm run build:main && npm run build:module", "build:main": "tsc -p tsconfig.json", "build:module": "tsc -p tsconfig.module.json", "test:auth": "npm run test:clean && npm run test:infra && npm run test:suite && npm run test:clean", diff --git a/packages/core/postgrest-js/package.json b/packages/core/postgrest-js/package.json index d71ad18bd..c10192cf2 100644 --- a/packages/core/postgrest-js/package.json +++ b/packages/core/postgrest-js/package.json @@ -37,7 +37,6 @@ "format": "node scripts/format.js", "format:check": "node scripts/format.js check", "build": "npm run clean && npm run build:cjs && npm run build:esm", - "postbuild": "cp -rf ./src/types/common ../supabase-js/src/lib/rest/types/common", "build:cjs": "tsc -p tsconfig.json", "build:esm": "cpy wrapper.mjs dist/esm/", "docs": "typedoc src/index.ts --out docs/v2", diff --git a/packages/core/storage-js/package.json b/packages/core/storage-js/package.json index 789519e13..40c3c3073 100644 --- a/packages/core/storage-js/package.json +++ b/packages/core/storage-js/package.json @@ -26,7 +26,7 @@ }, "scripts": { "clean": "rimraf dist docs/v2", - "build": "npx nx clean storage-js && npx nx build:main storage-js && npx nx build:module storage-js && npx nx build:umd storage-js", + "build": "npm run clean && npm run build:main && npm run build:module && npm run build:umd", "build:main": "tsc -p tsconfig.json", "build:module": "tsc -p tsconfig.module.json", "build:umd": "webpack", diff --git a/packages/core/supabase-js/package.json b/packages/core/supabase-js/package.json index e2c584be7..55b767779 100644 --- a/packages/core/supabase-js/package.json +++ b/packages/core/supabase-js/package.json @@ -26,12 +26,12 @@ }, "scripts": { "clean": "rimraf dist docs/v2", - "build": "npm run clean && npx nx build:main supabase-js && npx nx build:module supabase-js && npx nx build:umd supabase-js", + "build": "npm run clean && npm run build:main && npm run build:module && npm run build:umd", "build:main": "tsc -p tsconfig.json", "build:module": "tsc -p tsconfig.module.json", "build:umd": "webpack --env mode=production", - "test": "npx nx test:types supabase-js && npx nx test:run supabase-js", - "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", + "test": "npm run test:types && npm run test:run", + "test:all": "npm run test:types && npm run test:run && npm run test:integration && npm run test:integration:browser", "test:run": "jest --runInBand --detectOpenHandles", "test:unit": "jest --runInBand --detectOpenHandles test/unit", "test:coverage": "jest --runInBand --coverage --testPathIgnorePatterns=\"test/integration|test/deno\"", @@ -41,7 +41,7 @@ "test:watch": "jest --watch --verbose false --silent false", "test:node:playwright": "cd test/integration/node-browser && npm install && cp ../../../dist/umd/supabase.js . && npm run test", "test:bun": "cd test/integration/bun && bun install && bun test", - "test:types": "npx nx build:module supabase-js && tsd --files test/types/*.test-d.ts && jsr publish --dry-run --allow-dirty", + "test:types": "npm run build:module && tsd --files test/types/*.test-d.ts && jsr publish --dry-run --allow-dirty", "docs": "typedoc --entryPoints src/index.ts --out docs/v2", "docs:json": "typedoc --entryPoints src/index.ts --json docs/v2/spec.json --excludeExternals", "serve:coverage": "npx nx test:coverage supabase-js && serve test/coverage", diff --git a/packages/core/supabase-js/src/lib/rest/types/common/common.ts b/packages/core/supabase-js/src/lib/rest/types/common/common.ts index 9ad962ef5..8e38dd231 100644 --- a/packages/core/supabase-js/src/lib/rest/types/common/common.ts +++ b/packages/core/supabase-js/src/lib/rest/types/common/common.ts @@ -1,3 +1,13 @@ +/** + * AUTO-GENERATED FILE - DO NOT EDIT + * + * This file is automatically synchronized from @supabase/postgrest-js + * Source: packages/core/postgrest-js/src/types/common/ + * + * To update this file, modify the source in postgrest-js and run: + * npm run codegen + */ + // Types that are shared between supabase-js and postgrest-js export type Fetch = typeof fetch diff --git a/packages/core/supabase-js/src/lib/rest/types/common/rpc.ts b/packages/core/supabase-js/src/lib/rest/types/common/rpc.ts index 52e57419a..fe457bded 100644 --- a/packages/core/supabase-js/src/lib/rest/types/common/rpc.ts +++ b/packages/core/supabase-js/src/lib/rest/types/common/rpc.ts @@ -1,3 +1,13 @@ +/** + * AUTO-GENERATED FILE - DO NOT EDIT + * + * This file is automatically synchronized from @supabase/postgrest-js + * Source: packages/core/postgrest-js/src/types/common/ + * + * To update this file, modify the source in postgrest-js and run: + * npm run codegen + */ + import type { GenericFunction, GenericSchema, GenericSetofOption } from './common' // Functions matching utils diff --git a/scripts/sync-common-types.js b/scripts/sync-common-types.js new file mode 100755 index 000000000..467f8d2c6 --- /dev/null +++ b/scripts/sync-common-types.js @@ -0,0 +1,97 @@ +#!/usr/bin/env node + +/** + * Sync common type definitions from postgrest-js to supabase-js + * + * This script copies shared type definitions from @supabase/postgrest-js + * to @supabase/supabase-js to ensure type compatibility across complex + * generic types and conditional types. + * + * Source of truth: packages/core/postgrest-js/src/types/common/ + * Destination: packages/core/supabase-js/src/lib/rest/types/common/ + */ + +const fs = require('fs') +const path = require('path') + +const SOURCE_DIR = path.join(__dirname, '../packages/core/postgrest-js/src/types/common') +const DEST_DIR = path.join(__dirname, '../packages/core/supabase-js/src/lib/rest/types/common') + +const HEADER_COMMENT = `/** + * AUTO-GENERATED FILE - DO NOT EDIT + * + * This file is automatically synchronized from @supabase/postgrest-js + * Source: packages/core/postgrest-js/src/types/common/ + * + * To update this file, modify the source in postgrest-js and run: + * npm run codegen + */ + +` + +function syncFile(fileName) { + const sourcePath = path.join(SOURCE_DIR, fileName) + const destPath = path.join(DEST_DIR, fileName) + + if (!fs.existsSync(sourcePath)) { + console.error(`āŒ Source file not found: ${sourcePath}`) + process.exit(1) + } + + const sourceContent = fs.readFileSync(sourcePath, 'utf8') + const expectedContent = HEADER_COMMENT + sourceContent + + // Check if destination exists and compare with what we would generate + let needsUpdate = true + if (fs.existsSync(destPath)) { + const destContent = fs.readFileSync(destPath, 'utf8') + + // Compare the full expected output with the current file + if (destContent === expectedContent) { + needsUpdate = false + } + } + + if (needsUpdate) { + fs.writeFileSync(destPath, expectedContent, 'utf8') + console.log(`āœ… Synced: ${fileName}`) + return true + } else { + console.log(`ā­ļø Unchanged: ${fileName}`) + return false + } +} + +function main() { + console.log('šŸ”„ Syncing common types from postgrest-js to supabase-js...\n') + + // Ensure destination directory exists + if (!fs.existsSync(DEST_DIR)) { + fs.mkdirSync(DEST_DIR, { recursive: true }) + console.log(`šŸ“ Created directory: ${DEST_DIR}\n`) + } + + // Get all .ts files from source directory + const sourceFiles = fs.readdirSync(SOURCE_DIR).filter((file) => file.endsWith('.ts')) + + if (sourceFiles.length === 0) { + console.error(`āŒ No TypeScript files found in ${SOURCE_DIR}`) + process.exit(1) + } + + let changedCount = 0 + for (const file of sourceFiles) { + const changed = syncFile(file) + if (changed) changedCount++ + } + + console.log( + `\n✨ Sync complete: ${changedCount} file(s) updated, ${sourceFiles.length - changedCount} unchanged` + ) + + if (changedCount > 0) { + console.log('\nāš ļø Generated files were updated. Please commit these changes.') + } +} + +main()