36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
import process from "node:process";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { fileURLToPath, URL } from "node:url";
|
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
import { viewCommandSchema } from "../src/features/agent/presentation/command-schema.ts";
|
|
|
|
const sourceUrl = new URL("../src/features/agent/presentation/command-schema.ts", import.meta.url);
|
|
const source = await readFile(sourceUrl, "utf8");
|
|
const schema =
|
|
JSON.stringify(zodToJsonSchema(viewCommandSchema, { $refStrategy: "none" }), null, 2) + "\n";
|
|
const targets = [
|
|
[
|
|
new URL("../../next-tjwater-agent/src/frontendAction/command.ts", import.meta.url),
|
|
"// Generated by next-tjwater-frontend/scripts/export-view-command-contract.mjs.\n" + source
|
|
],
|
|
[
|
|
new URL(
|
|
"../../next-tjwater-agent/src/frontendAction/view-command.schema.json",
|
|
import.meta.url
|
|
),
|
|
schema
|
|
],
|
|
[new URL("../../next-tjwater-server/contracts/view-command.schema.json", import.meta.url), schema]
|
|
];
|
|
for (const [url, content] of targets) {
|
|
if (process.argv.includes("--check")) {
|
|
if ((await readFile(url, "utf8")) !== content)
|
|
throw new Error(`View command contract differs: ${fileURLToPath(url)}`);
|
|
} else {
|
|
await writeFile(url, content);
|
|
}
|
|
}
|
|
process.stdout.write(
|
|
`View command contract ${process.argv.includes("--check") ? "checked" : "exported"}.\n`
|
|
);
|