46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
import { spawnSync } from "node:child_process";
|
||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||
import { tmpdir } from "node:os";
|
||
import nodePath from "node:path";
|
||
|
||
const temporaryDirectory = await mkdtemp(
|
||
nodePath.join(tmpdir(), "wealth-ledger-openapi-"),
|
||
);
|
||
|
||
const generatedPath = nodePath.join(temporaryDirectory, "schema.d.ts");
|
||
const committedPath = nodePath.resolve("src/shared/api/generated/schema.d.ts");
|
||
const cliPath = nodePath.resolve("node_modules/openapi-typescript/bin/cli.js");
|
||
let exitCode = 0;
|
||
|
||
try {
|
||
const generated = spawnSync(
|
||
process.execPath,
|
||
[cliPath, "src/shared/api/spec/openapi.json", "-o", generatedPath],
|
||
{ encoding: "utf8" },
|
||
);
|
||
|
||
if (generated.status === 0) {
|
||
const [committed, current] = await Promise.all([
|
||
readFile(committedPath, "utf8"),
|
||
// eslint-disable-next-line security/detect-non-literal-fs-filename -- Path is built from this check’s private mkdtemp directory and fixed filenames.
|
||
readFile(generatedPath, "utf8"),
|
||
]);
|
||
|
||
if (committed !== current) {
|
||
console.error(
|
||
"Generated OpenAPI types are stale. Run `npm run openapi:generate` and commit the result.",
|
||
);
|
||
exitCode = 1;
|
||
}
|
||
}
|
||
else {
|
||
process.stderr.write(generated.stderr || generated.stdout);
|
||
exitCode = generated.status ?? 1;
|
||
}
|
||
}
|
||
finally {
|
||
await rm(temporaryDirectory, { force: true, recursive: true });
|
||
}
|
||
|
||
process.exitCode = exitCode;
|