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 parentPath = path.join(repoRoot, "tests/golden/M12-05D/runtime-receipts.json"); const outputArgument = process.argv.indexOf("--output"); const outputPath = outputArgument === -1 ? path.join(repoRoot, "tests/golden/M12-05E/bound-runtime-receipts.json") : path.resolve(process.argv[outputArgument + 1] ?? ""); if (!outputPath) throw new Error("--output requires a file"); const inventoryBytes = fs.readFileSync(inventoryPath); const parentBytes = fs.readFileSync(parentPath); const inventory = JSON.parse(inventoryBytes); const parent = JSON.parse(parentBytes); const canonical = (value) => value === null || typeof value !== "object" ? JSON.stringify(value) : Array.isArray(value) ? `[${value.map(canonical).join(",")}]` : `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}`; const hash = (value) => crypto.createHash("sha256").update(canonical(value)).digest("hex"); const runtimeSha256 = hash(inventory.runtime); const receipts = inventory.formats.flatMap((entry) => ["IMPORT", "EXPORT"].map((operation) => { const source = entry[operation.toLowerCase()]; const receipt = parent.receipts.find((item) => item.format === entry.format && item.operation === operation); return { ...receipt, sourceSha256: hash({ format: receipt.format, family: receipt.family, operation: receipt.operation, operator: receipt.operator, registered: receipt.registered, rnaIdentifier: receipt.rnaIdentifier }), settingsSha256: hash({ buildOption: receipt.buildOption, buildOptionEnabled: receipt.buildOptionEnabled, variants: receipt.variants, extensions: receipt.extensions, properties: source.properties }), runtimeSha256, }; })); const output = { schemaVersion: 1, task: "M12-05E", parentReceiptSetSha256: crypto.createHash("sha256").update(parentBytes).digest("hex"), 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-receipt-bindings-generated receipts=${receipts.length} output=${path.relative(repoRoot, outputPath)}\n`);