Skip to content

Commit 0e236d5

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 <[email protected]>
1 parent 709cacb commit 0e236d5

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 = [
16+
"src/generated/sdk.types.ts",
17+
"src/generated/sdk.schemas.ts",
18+
];
19+
20+
function main(): void {
21+
const rootDir = join(import.meta.dirname, "..");
22+
23+
// Capture current content of generated files
24+
const originalContents = new Map<string, string>();
25+
for (const file of GENERATED_FILES) {
26+
const filePath = join(rootDir, file);
27+
try {
28+
originalContents.set(file, readFileSync(filePath, "utf-8"));
29+
} catch {
30+
console.error(`Error: Generated file ${file} does not exist.`);
31+
console.error("Run 'npm run generate:schemas' to generate it.");
32+
process.exit(1);
33+
}
34+
}
35+
36+
// Regenerate schemas
37+
console.log("Regenerating schemas to check for drift...");
38+
try {
39+
execSync("npm run generate:schemas", {
40+
cwd: rootDir,
41+
stdio: "pipe",
42+
});
43+
} catch (error) {
44+
console.error("Error: Schema generation failed.");
45+
console.error((error as Error).message);
46+
process.exit(1);
47+
}
48+
49+
// Compare with original content
50+
let hasDrift = false;
51+
for (const file of GENERATED_FILES) {
52+
const filePath = join(rootDir, file);
53+
const newContent = readFileSync(filePath, "utf-8");
54+
const originalContent = originalContents.get(file)!;
55+
56+
if (newContent !== originalContent) {
57+
console.error(`\n❌ ${file} is out of sync with source types.`);
58+
hasDrift = true;
59+
} else {
60+
console.log(`✓ ${file} is up to date.`);
61+
}
62+
}
63+
64+
if (hasDrift) {
65+
console.error("\n" + "=".repeat(60));
66+
console.error("Generated schemas are out of sync!");
67+
console.error("Run 'npm run generate:schemas' and commit the changes.");
68+
console.error("=".repeat(60));
69+
process.exit(1);
70+
}
71+
72+
console.log("\n✓ All generated schemas are in sync.");
73+
}
74+
75+
main();

0 commit comments

Comments
 (0)