40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
/**
|
|
* B3.2 — fail when the generated API types are stale.
|
|
*
|
|
* Regenerates into a temporary file and compares. If this fails, the backend
|
|
* contract changed and `npm run types:api` has not been run — which is exactly
|
|
* the moment a field rename would otherwise reach a user at runtime.
|
|
*/
|
|
import { execFileSync } from 'node:child_process';
|
|
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
|
|
const COMMITTED = 'src/types/api/schema.d.ts';
|
|
const SOURCE = '../docqube_backend/openapi.json';
|
|
|
|
const dir = mkdtempSync(join(tmpdir(), 'docqube-types-'));
|
|
const fresh = join(dir, 'schema.d.ts');
|
|
|
|
try {
|
|
execFileSync('npx', ['openapi-typescript', SOURCE, '-o', fresh], {
|
|
stdio: 'pipe',
|
|
shell: process.platform === 'win32',
|
|
});
|
|
|
|
const a = readFileSync(COMMITTED, 'utf8').replace(/\r\n/g, '\n');
|
|
const b = readFileSync(fresh, 'utf8').replace(/\r\n/g, '\n');
|
|
|
|
if (a !== b) {
|
|
console.error(
|
|
'API types are stale.\n' +
|
|
`${COMMITTED} does not match what ${SOURCE} generates.\n` +
|
|
'Run: npm run types:api — then commit the diff.',
|
|
);
|
|
process.exit(1);
|
|
}
|
|
console.log('API types are current.');
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|