diff --git a/scripts/check-release-metadata-only.mjs b/scripts/check-release-metadata-only.mjs index 57e6ac3e7f7a..a068d8b10d93 100644 --- a/scripts/check-release-metadata-only.mjs +++ b/scripts/check-release-metadata-only.mjs @@ -29,23 +29,28 @@ function readRefOptionValue(argv, index, optionName) { } export function parseArgs(argv) { + const separatorIndex = argv.indexOf("--"); + const flagArgv = separatorIndex === -1 ? argv : argv.slice(0, separatorIndex); + const explicitPaths = + separatorIndex === -1 ? [] : argv.slice(separatorIndex + 1).map(normalizePath); const args = { staged: false, base: "origin/main", head: "HEAD", paths: [] }; - for (let index = 0; index < argv.length; index += 1) { - const arg = argv[index]; - if (arg === "--") { - continue; - } else if (arg === "--staged") { + for (let index = 0; index < flagArgv.length; index += 1) { + const arg = flagArgv[index]; + if (arg === "--staged") { args.staged = true; } else if (arg === "--base") { - args.base = readRefOptionValue(argv, index, arg); + args.base = readRefOptionValue(flagArgv, index, arg); index += 1; } else if (arg === "--head") { - args.head = readRefOptionValue(argv, index, arg); + args.head = readRefOptionValue(flagArgv, index, arg); index += 1; + } else if (arg.startsWith("-")) { + throw new Error(`Unknown option: ${arg}`); } else { args.paths.push(normalizePath(arg)); } } + args.paths.push(...explicitPaths); return args; } @@ -164,5 +169,10 @@ export function main(argv = process.argv.slice(2)) { } if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(import.meta.filename)) { - main(); + try { + main(); + } catch (error) { + console.error(error instanceof Error ? error.message : String(error)); + process.exit(1); + } } diff --git a/test/scripts/check-release-metadata-only.test.ts b/test/scripts/check-release-metadata-only.test.ts index 021ed12d1b0e..cc6daa50f562 100644 --- a/test/scripts/check-release-metadata-only.test.ts +++ b/test/scripts/check-release-metadata-only.test.ts @@ -25,4 +25,17 @@ describe("check-release-metadata-only", () => { expect(() => parseArgs(["--head"])).toThrow("Expected --head ."); expect(() => parseArgs(["--base", ""])).toThrow("Expected --base ."); }); + + it("rejects unknown options before treating args as paths", () => { + expect(() => parseArgs(["--stgaed"])).toThrow("Unknown option: --stgaed"); + }); + + it("preserves option-shaped paths after the separator", () => { + expect(parseArgs(["--staged", "--", "--head"])).toEqual({ + staged: true, + base: "origin/main", + head: "HEAD", + paths: ["--head"], + }); + }); });