Skip to content

Commit ac367a9

Browse files
ochafikclaude
andcommitted
ci: add schema sync check to build and CI
- Generate schemas during `npm run build` - Add `npm run check:schemas` script to verify generated files are in sync - Run schema sync check in CI after build to catch uncommitted changes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 709cacb commit ac367a9

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
- run: npm ci
2626
- run: npm run check
2727
- run: npm run build
28+
- run: npm run check:schemas
2829

2930
test:
3031
runs-on: ubuntu-latest

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"fetch:spec-types": "tsx scripts/fetch-spec-types.ts",
7171
"generate:schemas": "tsx scripts/generate-schemas.ts && prettier --write \"src/generated/**/*\"",
7272
"typecheck": "tsgo --noEmit",
73-
"build": "npm run build:esm && npm run build:cjs",
73+
"build": "npm run generate:schemas && npm run build:esm && npm run build:cjs",
7474
"build:esm": "mkdir -p dist/esm && echo '{\"type\": \"module\"}' > dist/esm/package.json && tsc -p tsconfig.prod.json",
7575
"build:esm:w": "npm run build:esm -- -w",
7676
"build:cjs": "mkdir -p dist/cjs && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json && tsc -p tsconfig.cjs.json",
@@ -79,7 +79,8 @@
7979
"prepack": "npm run build:esm && npm run build:cjs",
8080
"lint": "eslint src/ && prettier --check .",
8181
"lint:fix": "eslint src/ --fix && prettier --write .",
82-
"check": "npm run typecheck && npm run lint",
82+
"check": "npm run typecheck && npm run lint && npm run check:schemas",
83+
"check:schemas": "tsx scripts/check-schemas-sync.ts",
8384
"test": "vitest run",
8485
"test:watch": "vitest",
8586
"start": "npm run server",

scripts/check-schemas-sync.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)