import { readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { CI_CONTRACT_BASELINE_PATH, measureCiContractBaseline, parseCiGateContract, type CiContractBaseline, } from "./contracts/ci-gates.ts"; /** * D-13. Records the current `config/ci/gates.json` size and gate shape as the * reviewed baseline. Run it after an intended contract change; the diff of * `config/ci/contract-baseline.json` is what review approves. */ export async function updateCiContractBaseline( root: string, approval: Readonly<{ owner: string; reason: string }>, ): Promise { const contract = parseCiGateContract( JSON.parse(await readFile(path.join(root, "config/ci/gates.json"), "utf8")), ); const baseline: CiContractBaseline = { schemaVersion: 1, owner: approval.owner, reason: approval.reason, ...measureCiContractBaseline(contract), }; await writeFile( path.join(root, CI_CONTRACT_BASELINE_PATH), `${JSON.stringify(baseline, null, 2)}\n`, ); return baseline; } const isCli = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url); if (isCli) { const owner = process.env.CI_CONTRACT_BASELINE_OWNER; const reason = process.env.CI_CONTRACT_BASELINE_REASON; if (!owner || !reason) { process.stderr.write( "CI_CONTRACT_BASELINE_OWNER and CI_CONTRACT_BASELINE_REASON are required.\n", ); process.exit(1); } const baseline = await updateCiContractBaseline(process.cwd(), { owner, reason }); process.stdout.write( `CI contract baseline updated: ${baseline.gates} gates, ${baseline.commands} commands, ` + `${baseline.commandReferences} command references, ${baseline.artifacts} artifacts\n`, ); }