|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | +/** |
| 3 | + * Checks if generated schemas are in sync with source types. |
| 4 | + * Exits with code 1 if regeneration would produce different output. |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * npx tsx scripts/check-schemas-sync.ts |
| 8 | + * npm run check:schemas |
| 9 | + */ |
| 10 | + |
| 11 | +import { execSync } from 'child_process'; |
| 12 | +import { readFileSync } from 'fs'; |
| 13 | +import { join } from 'path'; |
| 14 | + |
| 15 | +const GENERATED_FILES = ['src/generated/sdk.types.ts', 'src/generated/sdk.schemas.ts']; |
| 16 | + |
| 17 | +function main(): void { |
| 18 | + const rootDir = join(import.meta.dirname, '..'); |
| 19 | + |
| 20 | + // Capture current content of generated files |
| 21 | + const originalContents = new Map<string, string>(); |
| 22 | + for (const file of GENERATED_FILES) { |
| 23 | + const filePath = join(rootDir, file); |
| 24 | + try { |
| 25 | + originalContents.set(file, readFileSync(filePath, 'utf-8')); |
| 26 | + } catch { |
| 27 | + console.error(`Error: Generated file ${file} does not exist.`); |
| 28 | + console.error("Run 'npm run generate:schemas' to generate it."); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Regenerate schemas |
| 34 | + console.log('Regenerating schemas to check for drift...'); |
| 35 | + try { |
| 36 | + execSync('npm run generate:schemas', { |
| 37 | + cwd: rootDir, |
| 38 | + stdio: 'pipe' |
| 39 | + }); |
| 40 | + } catch (error) { |
| 41 | + console.error('Error: Schema generation failed.'); |
| 42 | + console.error((error as Error).message); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + // Compare with original content |
| 47 | + let hasDrift = false; |
| 48 | + for (const file of GENERATED_FILES) { |
| 49 | + const filePath = join(rootDir, file); |
| 50 | + const newContent = readFileSync(filePath, 'utf-8'); |
| 51 | + const originalContent = originalContents.get(file)!; |
| 52 | + |
| 53 | + if (newContent !== originalContent) { |
| 54 | + console.error(`\n❌ ${file} is out of sync with source types.`); |
| 55 | + hasDrift = true; |
| 56 | + } else { |
| 57 | + console.log(`✓ ${file} is up to date.`); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (hasDrift) { |
| 62 | + console.error('\n' + '='.repeat(60)); |
| 63 | + console.error('Generated schemas are out of sync!'); |
| 64 | + console.error("Run 'npm run generate:schemas' and commit the changes."); |
| 65 | + console.error('='.repeat(60)); |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + console.log('\n✓ All generated schemas are in sync.'); |
| 70 | +} |
| 71 | + |
| 72 | +main(); |
0 commit comments