import process from "node:process"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import { dirname } from "node:path"; 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] ]; // The same executable examples and rejection matrix travel with each independent repository. for (const name of ["unified-presentation.jsonl", "chart-publication-cases.json"]) { const fixture = await readFile(new URL(`../tests/fixtures/${name}`, import.meta.url), "utf8"); for (const repository of ["next-tjwater-agent", "next-tjwater-server"]) targets.push([new URL(`../../${repository}/tests/fixtures/${name}`, import.meta.url), fixture]); } 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 mkdir(dirname(fileURLToPath(url)), { recursive: true }); await writeFile(url, content); } } process.stdout.write( `View command contract ${process.argv.includes("--check") ? "checked" : "exported"}.\n` );