This repository has been archived on 2026-10-29. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
EveConformance/tools/conformance/attach-plugin-witness.mjs
2026-07-10 10:30:19 +01:00

45 lines
2.3 KiB
JavaScript

import { mkdir, readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { validateSchemaSubset } from "../lib/schema-subset.mjs";
const [witnessArgument, exportArgument] = process.argv.slice(2);
if (!witnessArgument || !exportArgument) {
console.error("Usage: node tools/conformance/attach-plugin-witness.mjs <plugin-witness.json> <conformance-export-directory>");
process.exit(2);
}
const witnessPath = path.resolve(witnessArgument);
const exportDirectory = path.resolve(exportArgument);
const indexPath = path.join(exportDirectory, "index.json");
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const kernelRoot = path.resolve(process.env.EVE_KERNEL_ROOT || "E:\\Projects\\Eve");
const witness = parseJson(await readFile(witnessPath, "utf8"));
const schema = parseJson(await readFile(path.join(kernelRoot, "schemas/gamecult.eve.plugin_witness.v1.schema.json"), "utf8"));
const schemaErrors = validateSchemaSubset(schema, witness);
if (schemaErrors.length) throw new Error(`Plugin witness schema errors: ${schemaErrors.join("; ")}`);
const attachmentRoot = path.join(exportDirectory, "attachments", "plugin", safeSegment(witness.witnessId));
await mkdir(attachmentRoot, { recursive: true });
const exportedWitnessPath = path.join(attachmentRoot, "witness.json");
await writeFile(exportedWitnessPath, `${JSON.stringify(witness, null, 2)}\n`);
const index = parseJson(await readFile(indexPath, "utf8"));
const record = {
witnessId: witness.witnessId,
pluginId: witness.pluginId,
ownerRepo: witness.ownerRepo,
status: witness.status,
transport: witness.transport,
operationCount: witness.operations.length,
witnessExportPath: slash(path.relative(exportDirectory, exportedWitnessPath)),
};
index.pluginWitnesses = [
...(Array.isArray(index.pluginWitnesses) ? index.pluginWitnesses : []).filter(candidate => candidate.witnessId !== record.witnessId),
record,
].sort((left, right) => left.witnessId.localeCompare(right.witnessId));
await writeFile(indexPath, `${JSON.stringify(index, null, 2)}\n`);
console.log(exportedWitnessPath);
function safeSegment(value) { return String(value).replace(/[^a-zA-Z0-9._-]+/g, "-"); }
function slash(value) { return value.replaceAll("\\", "/"); }
function parseJson(value) { return JSON.parse(value.replace(/^\uFEFF/, "")); }