From af9d179e799ced01adf3b24cec392416ad07bb52 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Fri, 17 Feb 2023 13:39:14 +0000 Subject: [PATCH 1/3] Add tests --- examples/default-command-inverted.js | 11 +++++++++++ examples/default-command.js | 11 +++++++++++ src/__test__/index.test.ts | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 examples/default-command-inverted.js create mode 100644 examples/default-command.js diff --git a/examples/default-command-inverted.js b/examples/default-command-inverted.js new file mode 100644 index 00000000..e335c214 --- /dev/null +++ b/examples/default-command-inverted.js @@ -0,0 +1,11 @@ +require('ts-node/register') +const cli = require('../src/index').cac() + +const command = cli + .command("something", "Do something") + .alias("!") + .action(async () => { + console.log("Did something!"); + }); + +cli.parse() diff --git a/examples/default-command.js b/examples/default-command.js new file mode 100644 index 00000000..1275c61d --- /dev/null +++ b/examples/default-command.js @@ -0,0 +1,11 @@ +require('ts-node/register') +const cli = require('../src/index').cac() + +const command = cli + .command("", "Do something") + .alias("something") + .action(async () => { + console.log("Did something!"); + }); + +cli.parse() diff --git a/src/__test__/index.test.ts b/src/__test__/index.test.ts index 6cb84069..f1340215 100644 --- a/src/__test__/index.test.ts +++ b/src/__test__/index.test.ts @@ -176,3 +176,26 @@ describe('--version in help message', () => { expect(output).toContain(`--version`) }) }) + +describe("default commands", () => { + test("simple: empty call", async () => { + const output = await getOutput('default-command.js', []) + expect(output).toContain("Did something!") + }) + + test("simple: name alias call", async () => { + const output = await getOutput('default-command.js', ["something"]) + expect(output).toContain("Did something!") + }) + + // See https://github.com/cacjs/cac/issues/151 + test("inverted: empty call", async () => { + const output = await getOutput('default-command-inverted.js', []) + expect(output).toContain("Did something!") + }) + + test("inverted: name alias call", async () => { + const output = await getOutput('default-command-inverted.js', ["something"]) + expect(output).toContain("Did something!") + }) +}) From b0d49326dedc73911845abdaa6cb5aec4c1bc9c1 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Fri, 17 Feb 2023 13:41:05 +0000 Subject: [PATCH 2/3] Fix #151 --- src/CAC.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CAC.ts b/src/CAC.ts index 7a3df11a..20baa164 100644 --- a/src/CAC.ts +++ b/src/CAC.ts @@ -200,7 +200,7 @@ class CAC extends EventEmitter { if (shouldParse) { // Search the default command for (const command of this.commands) { - if (command.name === '') { + if (command.isDefaultCommand) { shouldParse = false const parsed = this.mri(argv.slice(2), command) this.setParsedInfo(parsed, command) From 5defaabd8140680edc6844066fc9fb6451d11221 Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Fri, 27 Feb 2026 23:34:55 +0900 Subject: [PATCH 3/3] merge --- examples/default-command-inverted.js | 11 ----------- examples/default-command-inverted.ts | 11 +++++++++++ examples/default-command.js | 11 ----------- examples/default-command.ts | 11 +++++++++++ tests/index.test.ts | 28 ++++++++++++++-------------- 5 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 examples/default-command-inverted.js create mode 100644 examples/default-command-inverted.ts delete mode 100644 examples/default-command.js create mode 100644 examples/default-command.ts diff --git a/examples/default-command-inverted.js b/examples/default-command-inverted.js deleted file mode 100644 index e335c214..00000000 --- a/examples/default-command-inverted.js +++ /dev/null @@ -1,11 +0,0 @@ -require('ts-node/register') -const cli = require('../src/index').cac() - -const command = cli - .command("something", "Do something") - .alias("!") - .action(async () => { - console.log("Did something!"); - }); - -cli.parse() diff --git a/examples/default-command-inverted.ts b/examples/default-command-inverted.ts new file mode 100644 index 00000000..7e5ca702 --- /dev/null +++ b/examples/default-command-inverted.ts @@ -0,0 +1,11 @@ +import { cac } from '../src/index.ts' +const cli = cac() + +cli + .command('something', 'Do something') + .alias('!') + .action(() => { + console.info('Did something!') + }) + +cli.parse() diff --git a/examples/default-command.js b/examples/default-command.js deleted file mode 100644 index 1275c61d..00000000 --- a/examples/default-command.js +++ /dev/null @@ -1,11 +0,0 @@ -require('ts-node/register') -const cli = require('../src/index').cac() - -const command = cli - .command("", "Do something") - .alias("something") - .action(async () => { - console.log("Did something!"); - }); - -cli.parse() diff --git a/examples/default-command.ts b/examples/default-command.ts new file mode 100644 index 00000000..f160f898 --- /dev/null +++ b/examples/default-command.ts @@ -0,0 +1,11 @@ +import { cac } from '../src/index.ts' +const cli = cac() + +cli + .command('', 'Do something') + .alias('something') + .action(() => { + console.info('Did something!') + }) + +cli.parse() diff --git a/tests/index.test.ts b/tests/index.test.ts index ca2c58db..bbb176b4 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -187,25 +187,25 @@ describe('--version in help message', () => { }) }) -describe("default commands", () => { - test("simple: empty call", async () => { - const output = await getOutput('default-command.js', []) - expect(output).toContain("Did something!") +describe('default commands', () => { + test('simple: empty call', async () => { + const output = await getOutput('default-command.ts', []) + expect(output).toContain('Did something!') }) - test("simple: name alias call", async () => { - const output = await getOutput('default-command.js', ["something"]) - expect(output).toContain("Did something!") + test('simple: name alias call', async () => { + const output = await getOutput('default-command.ts', ['something']) + expect(output).toContain('Did something!') }) // See https://github.com/cacjs/cac/issues/151 - test("inverted: empty call", async () => { - const output = await getOutput('default-command-inverted.js', []) - expect(output).toContain("Did something!") + test('inverted: empty call', async () => { + const output = await getOutput('default-command-inverted.ts', []) + expect(output).toContain('Did something!') }) - - test("inverted: name alias call", async () => { - const output = await getOutput('default-command-inverted.js', ["something"]) - expect(output).toContain("Did something!") + + test('inverted: name alias call', async () => { + const output = await getOutput('default-command-inverted.ts', ['something']) + expect(output).toContain('Did something!') }) })