37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
import { spawn } from "node:child_process";
|
|
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
const snapshot = "src/shared/api/spec/openapi.json";
|
|
const generated = "src/shared/api/generated/schema.d.ts";
|
|
const directory = await mkdtemp(join(tmpdir(), "wealth-ledger-openapi-"));
|
|
const expected = join(directory, "schema.d.ts");
|
|
|
|
try {
|
|
await new Promise((resolve, reject) => {
|
|
const child = spawn(
|
|
"npx",
|
|
["--no-install", "openapi-typescript", snapshot, "-o", expected],
|
|
{
|
|
stdio: "inherit",
|
|
},
|
|
);
|
|
child.on("error", reject);
|
|
child.on("exit", (code) =>
|
|
code === 0
|
|
? resolve()
|
|
: reject(new Error(`openapi-typescript exited with ${code}`)),
|
|
);
|
|
});
|
|
if (
|
|
(await readFile(expected, "utf8")) !== (await readFile(generated, "utf8"))
|
|
) {
|
|
throw new Error(
|
|
"Generated OpenAPI types drift from src/shared/api/spec/openapi.json. Run npm run openapi:generate.",
|
|
);
|
|
}
|
|
} finally {
|
|
await rm(directory, { recursive: true, force: true });
|
|
}
|