44 lines
2.4 KiB
JavaScript
44 lines
2.4 KiB
JavaScript
import crypto from "node:crypto";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
import ts from "../../web/node_modules/typescript/lib/typescript.js";
|
|
|
|
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
|
const matrixPath = path.join(repoRoot, "tests/golden/M12-05B/capability-matrix.json");
|
|
const protocolPath = path.join(repoRoot, "web/protocol/io-format-capability-matrix.ts");
|
|
const gatePath = path.join(repoRoot, "web/protocol/io-format-ui-gate.ts");
|
|
const output = process.argv[process.argv.indexOf("--output") + 1];
|
|
if (!output || output.startsWith("--")) throw new Error("usage: node generate-io-format-ui-gate.mjs --output <path>");
|
|
|
|
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "io-format-ui-gate-"));
|
|
try {
|
|
const transpile = (sourcePath, outputName) => {
|
|
const result = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), {
|
|
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
|
|
fileName: sourcePath,
|
|
reportDiagnostics: true,
|
|
});
|
|
if (result.diagnostics?.length) throw new Error(ts.formatDiagnosticsWithColorAndContext(result.diagnostics, { getCanonicalFileName: (file) => file, getCurrentDirectory: () => repoRoot, getNewLine: () => "\n" }));
|
|
const target = path.join(temporary, outputName);
|
|
fs.writeFileSync(target, result.outputText);
|
|
return target;
|
|
};
|
|
const matrixModulePath = transpile(protocolPath, "io-format-capability-matrix.mjs");
|
|
const gateModulePath = transpile(gatePath, "io-format-ui-gate.mjs");
|
|
const matrixModule = await import(pathToFileURL(matrixModulePath));
|
|
const gateModule = await import(pathToFileURL(gateModulePath));
|
|
const matrixBytes = fs.readFileSync(matrixPath);
|
|
const matrix = matrixModule.parseIOFormatCapabilityMatrix(JSON.parse(matrixBytes));
|
|
const registry = gateModule.buildIOFormatUIRegistry(matrix, crypto.createHash("sha256").update(matrixBytes).digest("hex"));
|
|
const target = path.resolve(repoRoot, output);
|
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
fs.writeFileSync(target, `${JSON.stringify(registry, null, 2)}\n`);
|
|
process.stdout.write(`io-format-ui-gate-generated output=${path.relative(repoRoot, target)} import=${registry.importRoutes.length} export=${registry.exportRoutes.length}\n`);
|
|
}
|
|
finally {
|
|
fs.rmSync(temporary, { recursive: true, force: true });
|
|
}
|
|
|