|
| 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