import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const inventoryPath = path.join(repoRoot, "tests/golden/M12-05A/format-inventory.json"); const outputArgument = process.argv.indexOf("--output"); const outputPath = outputArgument === -1 ? path.join(repoRoot, "tests/golden/M12-05D/runtime-receipts.json") : path.resolve(process.argv[outputArgument + 1] ?? ""); if (!outputPath) throw new Error("--output requires a file"); const inventoryBytes = fs.readFileSync(inventoryPath); const inventory = JSON.parse(inventoryBytes); if (inventory.schemaVersion !== 1 || inventory.task !== "M12-05A") throw new Error("M12-05A inventory header is invalid"); const receipts = inventory.formats.flatMap((entry) => ["IMPORT", "EXPORT"].map((operation) => { const source = entry[operation.toLowerCase()]; return { format: entry.format, family: entry.family, operation, operator: source.operator, registered: source.registered, rnaIdentifier: source.rnaIdentifier, buildOption: source.buildOption, buildOptionEnabled: source.buildOptionEnabled, runtimeStatus: source.runtimeStatus, variants: [...entry.variants], extensions: [...entry.extensions], }; })); const output = { schemaVersion: 1, task: "M12-05D", inventorySha256: crypto.createHash("sha256").update(inventoryBytes).digest("hex"), runtime: inventory.runtime, receipts, }; fs.mkdirSync(path.dirname(outputPath), { recursive: true }); fs.writeFileSync(outputPath, `${JSON.stringify(output, null, 2)}\n`); process.stdout.write(`io-format-runtime-receipts-generated formats=${inventory.formats.length} receipts=${receipts.length} output=${path.relative(repoRoot, outputPath)}\n`);