feat: add deterministic Diagram IR v2 authoring
This commit is contained in:
@@ -28,10 +28,11 @@ import * as yaml from "js-yaml";
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type ActionModule = {
|
||||
run: (filePath: string, pageIndex?: number) => Record<string, unknown>;
|
||||
run: (filePath: string, pageIndex?: number, outputPath?: string) => Record<string, unknown>;
|
||||
};
|
||||
|
||||
const ACTIONS: Record<string, () => Promise<ActionModule>> = {
|
||||
"build": () => import("../actions/build/action.js"),
|
||||
"summary": () => import("../actions/summary/action.js"),
|
||||
"page-summary": () => import("../actions/page-summary/action.js"),
|
||||
"page-hierarchy": () => import("../actions/page-hierarchy/action.js"),
|
||||
@@ -44,6 +45,7 @@ const ACTIONS: Record<string, () => Promise<ActionModule>> = {
|
||||
"page-hierarchy-full": () => import("../actions/page-hierarchy-full/action.js"),
|
||||
"page-negative-space-summary":() => import("../actions/page-negative-space-summary/action.js"),
|
||||
"validate": () => import("../actions/validate/action.js"),
|
||||
"quality": () => import("../actions/quality/action.js"),
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -51,7 +53,7 @@ const ACTIONS: Record<string, () => Promise<ActionModule>> = {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function main(): Promise<void> {
|
||||
let values: { file?: string; action?: string; page?: string; help?: boolean };
|
||||
let values: { file?: string; action?: string; page?: string; output?: string; help?: boolean };
|
||||
try {
|
||||
({ values } = parseArgs({
|
||||
args: process.argv.slice(2),
|
||||
@@ -59,6 +61,7 @@ async function main(): Promise<void> {
|
||||
file: { type: "string", short: "f" },
|
||||
action: { type: "string", short: "a" },
|
||||
page: { type: "string", short: "p" },
|
||||
output: { type: "string", short: "o" },
|
||||
help: { type: "boolean", short: "h" },
|
||||
},
|
||||
strict: true,
|
||||
@@ -69,7 +72,7 @@ async function main(): Promise<void> {
|
||||
}
|
||||
|
||||
if (values.help) {
|
||||
console.log(`drawio-tools --file <path> --action <action> [--page <index>]
|
||||
console.log(`drawio-tools --file <path> --action <action> [--page <index>] [--output <path>]
|
||||
|
||||
Actions: ${Object.keys(ACTIONS).join(", ")}`);
|
||||
process.exit(0);
|
||||
@@ -90,8 +93,12 @@ Actions: ${Object.keys(ACTIONS).join(", ")}`);
|
||||
// Parse optional --page argument (0-based index, default 0)
|
||||
let pageIndex = 0;
|
||||
if (values.page !== undefined) {
|
||||
pageIndex = parseInt(values.page, 10);
|
||||
if (isNaN(pageIndex) || pageIndex < 0) {
|
||||
if (!/^\d+$/.test(values.page)) {
|
||||
printError(`--page must be a non-negative integer, got: ${values.page}`);
|
||||
process.exit(1);
|
||||
}
|
||||
pageIndex = Number(values.page);
|
||||
if (!Number.isSafeInteger(pageIndex)) {
|
||||
printError(`--page must be a non-negative integer, got: ${values.page}`);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -106,10 +113,14 @@ Actions: ${Object.keys(ACTIONS).join(", ")}`);
|
||||
|
||||
try {
|
||||
const mod = await ACTIONS[actionName]();
|
||||
const result = mod.run(filePath, pageIndex);
|
||||
const result = mod.run(filePath, pageIndex, values.output);
|
||||
process.stdout.write(
|
||||
yaml.dump(result, { noRefs: true, sortKeys: false, lineWidth: 120 })
|
||||
);
|
||||
const summary = result.summary;
|
||||
if (actionName === "validate" && summary && typeof summary === "object" && (summary as Record<string, unknown>).valid === false) {
|
||||
process.exitCode = 1;
|
||||
}
|
||||
} catch (err) {
|
||||
const errorOutput = {
|
||||
error: true,
|
||||
@@ -126,7 +137,7 @@ Actions: ${Object.keys(ACTIONS).join(", ")}`);
|
||||
function printError(msg: string): void {
|
||||
process.stderr.write(`Error: ${msg}\n`);
|
||||
process.stderr.write(
|
||||
`Usage: drawio-tools --file <path> --action <${Object.keys(ACTIONS).join("|")}> [--page <index>]\n`
|
||||
`Usage: drawio-tools --file <path> --action <${Object.keys(ACTIONS).join("|")}> [--page <index>] [--output <path>]\n`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user