From de10288d6e1fc532cbf94fa61c99fb34d02fe9eb Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 29 Apr 2024 15:09:45 +0200 Subject: [PATCH] fix: unknown command error not showing with package.json (#82) * fix: unknown command error not showing with package.json * chore: remove debug log * fix --- src/bin.ts | 14 ++++++++++---- test/commands.test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 9167cea..04e6d61 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -249,11 +249,10 @@ if (args.length === 0) { await runScript(process.cwd(), cmd, { pkgManagerName }); }); } else { - console.error(kl.red(`Unknown command: ${cmd}`)); - console.log(); - printHelp(); - process.exit(1); + throwUnknownCommand(cmd); } + } else { + throwUnknownCommand(cmd); } } } @@ -278,3 +277,10 @@ async function run(fn: () => Promise) { throw err; } } + +function throwUnknownCommand(cmd: string) { + console.error(kl.red(`Unknown command: ${cmd}`)); + console.log(); + printHelp(); + process.exit(1); +} diff --git a/test/commands.test.ts b/test/commands.test.ts index c8c9a28..b2e5c2c 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -21,6 +21,39 @@ import { writeTextFile, } from "../src/utils"; +describe("general", () => { + it("exit 1 on unknown command", async () => { + try { + await withTempEnv(["foo"], async () => {}); + assert.fail("no"); + } catch (err) { + if (err instanceof Error) { + assert.match(err.message, /Child process/); + assert.equal((err as any).code, 1); + } else { + throw err; + } + } + }); + + // See https://github.com/jsr-io/jsr-npm/issues/79 + it("exit 1 on unknown command in empty folder", async () => { + await runInTempDir(async (dir) => { + try { + await runJsr(["asdf"], dir); + assert.fail("no"); + } catch (err) { + if (err instanceof Error) { + assert.match(err.message, /Child process/); + assert.equal((err as any).code, 1); + } else { + throw err; + } + } + }); + }); +}); + describe("install", () => { it("jsr i @std/encoding - resolve latest version", async () => { await withTempEnv(["i", "@std/encoding"], async (dir) => {