Skip to content

Commit de10288

Browse files
fix: unknown command error not showing with package.json (#82)
* fix: unknown command error not showing with package.json * chore: remove debug log * fix
1 parent cdda834 commit de10288

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/bin.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,10 @@ if (args.length === 0) {
249249
await runScript(process.cwd(), cmd, { pkgManagerName });
250250
});
251251
} else {
252-
console.error(kl.red(`Unknown command: ${cmd}`));
253-
console.log();
254-
printHelp();
255-
process.exit(1);
252+
throwUnknownCommand(cmd);
256253
}
254+
} else {
255+
throwUnknownCommand(cmd);
257256
}
258257
}
259258
}
@@ -278,3 +277,10 @@ async function run(fn: () => Promise<void>) {
278277
throw err;
279278
}
280279
}
280+
281+
function throwUnknownCommand(cmd: string) {
282+
console.error(kl.red(`Unknown command: ${cmd}`));
283+
console.log();
284+
printHelp();
285+
process.exit(1);
286+
}

test/commands.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,39 @@ import {
2121
writeTextFile,
2222
} from "../src/utils";
2323

24+
describe("general", () => {
25+
it("exit 1 on unknown command", async () => {
26+
try {
27+
await withTempEnv(["foo"], async () => {});
28+
assert.fail("no");
29+
} catch (err) {
30+
if (err instanceof Error) {
31+
assert.match(err.message, /Child process/);
32+
assert.equal((err as any).code, 1);
33+
} else {
34+
throw err;
35+
}
36+
}
37+
});
38+
39+
// See https://github.com/jsr-io/jsr-npm/issues/79
40+
it("exit 1 on unknown command in empty folder", async () => {
41+
await runInTempDir(async (dir) => {
42+
try {
43+
await runJsr(["asdf"], dir);
44+
assert.fail("no");
45+
} catch (err) {
46+
if (err instanceof Error) {
47+
assert.match(err.message, /Child process/);
48+
assert.equal((err as any).code, 1);
49+
} else {
50+
throw err;
51+
}
52+
}
53+
});
54+
});
55+
});
56+
2457
describe("install", () => {
2558
it("jsr i @std/encoding - resolve latest version", async () => {
2659
await withTempEnv(["i", "@std/encoding"], async (dir) => {

0 commit comments

Comments
 (0)