guanghu-ice-heart/server-tools/ai-discovery-gateway/validate-public-snapshot.js

45 lines
1.6 KiB
JavaScript
Executable file

#!/usr/bin/env node
"use strict";
const path = require("node:path");
const { GitSnapshotStore, loadFileSnapshot } = require("./server");
function reminder(error) {
const code = String(error?.message || error);
const match = code.match(/snapshot_map_version_mismatch:([a-z0-9_]+)/i);
if (match) {
return `${code}\nPUBLIC_SNAPSHOT_REMINDER: ${match[1]} 的地图版本与 routing/public-navigation-anchor.json 声明不一致;发布前必须同步锚点声明。`;
}
return `${code}\nPUBLIC_SNAPSHOT_REMINDER: 公共快照契约未通过;请同步锚点、地图路径、编号和版本后再发布。`;
}
function validate(options = {}) {
if (options.gitDir) {
const store = new GitSnapshotStore(path.resolve(options.gitDir), options.ref || "HEAD");
const snapshot = store.get();
return { result: "PASS_100", anchor_version: snapshot.anchor.version, source_commit: snapshot.source_commit };
}
const snapshot = loadFileSnapshot();
return { result: "PASS_100", anchor_version: snapshot.anchor.version, source_commit: null };
}
function parse(argv) {
const options = {};
for (let i = 0; i < argv.length; i += 1) {
if (argv[i] === "--git-dir") options.gitDir = argv[++i];
else if (argv[i] === "--ref") options.ref = argv[++i];
else throw new Error(`UNKNOWN_ARGUMENT:${argv[i]}`);
}
return options;
}
if (require.main === module) {
try {
process.stdout.write(`${JSON.stringify(validate(parse(process.argv.slice(2))))}\n`);
} catch (error) {
process.stderr.write(`${reminder(error)}\n`);
process.exitCode = 1;
}
}
module.exports = { reminder, validate };