diff --git a/src/cli.mjs b/src/cli.mjs index 65afd36b..b15faa7c 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -64,6 +64,32 @@ function buildYargs(argvInput) { // https://github.com/yargs/yargs/blob/main/docs/typescript.md?plain=1#L124 const yargsInstance = yargs(argvInput); + // these debug commands are used by the tests in environments where they can't mock out the command handler + if ( + process.env.NODE_ENV !== "production" || + process.env.DEBUG_COMMANDS === "true" + ) { + yargsInstance + .command("throw", false, { + handler: () => { + throw new Error("this is a test error"); + }, + builder: {}, + }) + .command("reject", false, { + handler: async () => { + throw new Error("this is a rejected promise"); + }, + builder: {}, + }) + .command("warn", false, { + handler: async () => { + process.emitWarning("this is a warning emited on the node process"); + }, + builder: {}, + }); + } + return yargsInstance .scriptName("fauna") .middleware([checkForUpdates, logArgv], true) @@ -74,24 +100,6 @@ function buildYargs(argvInput) { .command(keyCommand) .command(schemaCommand) .command(databaseCommand) - .command("throw", false, { - handler: () => { - throw new Error("this is a test error"); - }, - builder: {}, - }) - .command("reject", false, { - handler: async () => { - throw new Error("this is a rejected promise"); - }, - builder: {}, - }) - .command("warn", false, { - handler: async () => { - process.emitWarning("this is a warning emited on the node process"); - }, - builder: {}, - }) .demandCommand() .strict(true) .options({ diff --git a/test/general-cli.mjs b/test/general-cli.mjs index 9fa4a0a5..bc70ef08 100644 --- a/test/general-cli.mjs +++ b/test/general-cli.mjs @@ -127,14 +127,21 @@ describe("cli operations", function () { expect(notify).to.have.been.called; }); + it("does not expose debug commands in production", async function () { + const cliPath = path.resolve(__dirname, "../dist/cli.cjs"); + const { stderr } = spawnSync(cliPath, ["throw"], { + encoding: "utf8", + timeout: 5000, + }); + + expect(stderr).to.include("Unknown argument: throw"); + }); + it("enables nodeJS warnings from the dev entrypoint", async function () { const cliPath = path.resolve(__dirname, "../src/user-entrypoint.mjs"); let cli = spawnSync(cliPath, ["warn"], { encoding: "utf8", - // input: "", timeout: 5000, - // stdio: ["inherit", "pipe", "pipe"], - // shell: true, }); if (cli.error) throw cli.error; let stderr = cli.stderr; @@ -149,10 +156,11 @@ describe("cli operations", function () { const cliPath = path.resolve(__dirname, "../dist/cli.cjs"); let cli = spawnSync(cliPath, ["warn"], { encoding: "utf8", - // input: "", timeout: 5000, - // stdio: ["inherit", "pipe", "pipe"], - // shell: true, + env: { + ...process.env, + DEBUG_COMMANDS: "true", + }, }); if (cli.error) throw cli.error;