Skip to content

Commit 28c1c8a

Browse files
committed
fix: harden CLI router errors and flag handling
1 parent e63b427 commit 28c1c8a

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎src/cli.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const COMMAND_HELP: Record<CommandName, string> = {
3131
};
3232

3333
const cli = cac("skillet");
34+
const GLOBAL_FLAGS = new Set(["-y", "--yes", "--verbose", "-v", "--version", "-h", "--help"]);
3435

3536
cli
3637
.option("-y, --yes", "Skip confirmations")
@@ -67,13 +68,42 @@ for (const command of COMMANDS) {
6768
addCommand(command);
6869
}
6970

70-
cli.on("command:*", (commands) => {
71-
console.error(`Unknown command: ${commands.join(" ")}`);
71+
cli.on("command:*", (commands?: string[]) => {
72+
const unknown = commands?.join(" ") || process.argv.slice(2).join(" ");
73+
console.error(`Unknown command: ${unknown}`);
7274
cli.outputHelp();
7375
process.exit(1);
7476
});
7577

76-
if (process.argv.length <= 2) {
78+
function findUnknownGlobalFlag(argv: string[]): string | undefined {
79+
for (const token of argv) {
80+
if (token === "--") {
81+
return undefined;
82+
}
83+
84+
if (!token.startsWith("-")) {
85+
return undefined;
86+
}
87+
88+
const flag = token.includes("=") ? token.slice(0, token.indexOf("=")) : token;
89+
if (!GLOBAL_FLAGS.has(flag)) {
90+
return flag;
91+
}
92+
}
93+
94+
return undefined;
95+
}
96+
97+
const rawArgs = process.argv.slice(2);
98+
const unknownGlobalFlag = findUnknownGlobalFlag(rawArgs);
99+
100+
if (unknownGlobalFlag) {
101+
console.error(`Unknown option: ${unknownGlobalFlag}`);
102+
cli.outputHelp();
103+
process.exit(1);
104+
}
105+
106+
if (rawArgs.length === 0) {
77107
cli.outputHelp();
78108
} else {
79109
cli.parse();

‎tests/cli.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { spawnSync } from "node:child_process";
2+
import path from "node:path";
3+
import { describe, expect, test } from "vitest";
4+
5+
function runCli(args: string[]) {
6+
const cliPath = path.resolve(process.cwd(), "src/cli.ts");
7+
return spawnSync("bun", [cliPath, ...args], {
8+
cwd: process.cwd(),
9+
encoding: "utf8",
10+
});
11+
}
12+
13+
describe("cli", () => {
14+
test("shows root help with command list", () => {
15+
const result = runCli(["--help"]);
16+
17+
expect(result.status).toBe(0);
18+
expect(result.stdout).toContain("Commands:");
19+
expect(result.stdout).toContain("add [...args]");
20+
expect(result.stdout).toContain("generate-lock [...args]");
21+
});
22+
23+
test("shows subcommand help", () => {
24+
const result = runCli(["find", "--help"]);
25+
26+
expect(result.status).toBe(0);
27+
expect(result.stdout).toContain("Usage:");
28+
expect(result.stdout).toContain("$ skillet find [...args]");
29+
});
30+
31+
test("prints version from environment override", () => {
32+
const cliPath = path.resolve(process.cwd(), "src/cli.ts");
33+
const result = spawnSync("bun", [cliPath, "--version"], {
34+
cwd: process.cwd(),
35+
encoding: "utf8",
36+
env: { ...process.env, SKILLET_VERSION: "1.2.3" },
37+
});
38+
39+
expect(result.status).toBe(0);
40+
expect(result.stdout).toContain("skillet/1.2.3");
41+
});
42+
43+
test("returns clear error for unknown command", () => {
44+
const result = runCli(["nope"]);
45+
46+
expect(result.status).not.toBe(0);
47+
expect(result.stderr).toContain("Unknown command: nope");
48+
expect(result.stderr).not.toContain("TypeError");
49+
});
50+
51+
test("returns non-zero with clear message for invalid global flag", () => {
52+
const result = runCli(["--bogus"]);
53+
54+
expect(result.status).not.toBe(0);
55+
expect(result.stderr).toContain("Unknown option: --bogus");
56+
});
57+
});

0 commit comments

Comments
 (0)