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