diff --git a/scripts/openclaw-performance-source-summary.mjs b/scripts/openclaw-performance-source-summary.mjs index b480ae3bae2..173f74d12cb 100644 --- a/scripts/openclaw-performance-source-summary.mjs +++ b/scripts/openclaw-performance-source-summary.mjs @@ -4,16 +4,22 @@ import fs from "node:fs"; import path from "node:path"; import process from "node:process"; +import { pathToFileURL } from "node:url"; -function parseArgs(argv) { +function readOptionValue(argv, index, optionName) { + const value = argv[index + 1]; + if (!value || value.startsWith("--")) { + throw new Error(`${optionName} requires a value`); + } + return value; +} + +export function parseArgs(argv) { const options = { baselineSourceDir: null, sourceDir: null, output: null }; for (let index = 0; index < argv.length; index += 1) { const arg = argv[index]; const readValue = () => { - const value = argv[index + 1]; - if (!value) { - throw new Error(`Missing value for ${arg}`); - } + const value = readOptionValue(argv, index, arg); index += 1; return value; }; @@ -445,9 +451,16 @@ async function main() { } } -main().catch( - /** @param {unknown} error */ (error) => { - console.error(error instanceof Error ? error.stack : String(error)); - process.exitCode = 1; - }, -); +function isCliEntry() { + const cliArg = process.argv[1]; + return cliArg ? import.meta.url === pathToFileURL(cliArg).href : false; +} + +if (isCliEntry()) { + main().catch( + /** @param {unknown} error */ (error) => { + console.error(error instanceof Error ? error.stack : String(error)); + process.exitCode = 1; + }, + ); +} diff --git a/test/scripts/openclaw-performance-source-summary.test.ts b/test/scripts/openclaw-performance-source-summary.test.ts new file mode 100644 index 00000000000..d37cd804448 --- /dev/null +++ b/test/scripts/openclaw-performance-source-summary.test.ts @@ -0,0 +1,32 @@ +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { parseArgs } from "../../scripts/openclaw-performance-source-summary.mjs"; + +describe("parseArgs", () => { + it("parses source summary paths", () => { + expect( + parseArgs([ + "--source-dir", + "reports/current", + "--baseline-source-dir", + "reports/baseline", + "--output", + "summary.md", + ]), + ).toEqual({ + sourceDir: path.resolve("reports/current"), + baselineSourceDir: path.resolve("reports/baseline"), + output: path.resolve("summary.md"), + }); + }); + + it("rejects missing path values", () => { + for (const flag of ["--source-dir", "--baseline-source-dir", "--output"]) { + expect(() => parseArgs([flag])).toThrow(`${flag} requires a value`); + expect(() => parseArgs([flag, ""])).toThrow(`${flag} requires a value`); + expect(() => parseArgs([flag, "--source-dir", "reports/current"])).toThrow( + `${flag} requires a value`, + ); + } + }); +});