From c2b4450bb9aa08dfc661dbd07d3b787bc653cfac Mon Sep 17 00:00:00 2001 From: Cleve Stuart Date: Fri, 22 Nov 2024 16:18:04 -0500 Subject: [PATCH] If no command is given show help text; and instructions on sub-help. Include a dev quick start in the README --- DEV-README.md | 10 ++++++++++ src/cli.mjs | 9 ++++++--- test/general-cli.mjs | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/DEV-README.md b/DEV-README.md index 72c38bb4..24c9bea0 100644 --- a/DEV-README.md +++ b/DEV-README.md @@ -1,3 +1,13 @@ +### Quick Start + +``` +npm install +npm run build +./src/user-entrypoint.mjs +``` + + + ### Application versions This project has 3 runnable entrypoints (a raw ESM one, a built CJS one, and an SEA one). You can read more about them [here](./sea/README.md). diff --git a/src/cli.mjs b/src/cli.mjs index c8ac30e8..d634e66e 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -36,9 +36,12 @@ export async function run(argvInput, _container) { builtYargs = buildYargs(argvInput); await parseYargs(builtYargs); } catch (e) { - const message = `${chalk.reset(await builtYargs.getHelp())}\n\n${chalk.red( - e.message, - )}`; + let subMessage = chalk.reset("Use 'fauna --help' for more information about a command."); + + if (argvInput.length > 0) { + subMessage = chalk.red(e.message); + } + const message = `${chalk.reset(await builtYargs.getHelp())}\n\n${subMessage}`; logger.stderr(message); logger.fatal(e.stack, "error"); const exitCode = e.exitCode !== undefined ? e.exitCode : 1; diff --git a/test/general-cli.mjs b/test/general-cli.mjs index bc70ef08..de766e0b 100644 --- a/test/general-cli.mjs +++ b/test/general-cli.mjs @@ -40,7 +40,7 @@ describe("cli operations", function () { }); // TODO: this doesn't work because turning on strict mode breaks parsing sub-commands. why? - it("should exit with a helpful message if a non-existant command is provided", async function () { + it("should exit with a helpful message if a non-existent command is provided", async function () { const logger = container.resolve("logger"); // this command does not exist @@ -56,6 +56,22 @@ describe("cli operations", function () { expect(container.resolve("parseYargs")).to.have.been.calledOnce; }); + it("should exit with a helpful message if no command is provided", async function () { + const logger = container.resolve("logger"); + + // no input + try { + await run("", container); + } catch (e) {} + + expect(logger.stdout).to.not.be.called; + const message = `${chalk.reset(await builtYargs.getHelp())}\n\n${chalk.reset( + "Use 'fauna --help' for more information about a command.", + )}`; + expect(logger.stderr).to.have.been.calledWith(message); + expect(container.resolve("parseYargs")).to.have.been.calledOnce; + }); + it("should exit with a helpful message if the handler throws", async function () { const logger = container.resolve("logger");