Skip to content

Commit 28ddf47

Browse files
domdomeggsxzz
andauthored
fix: check default commands (#152)
* Add tests * Fix #151 * merge --------- Co-authored-by: Kevin Deng <sxzz@sxzz.moe>
1 parent 4a83b21 commit 28ddf47

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { cac } from '../src/index.ts'
2+
const cli = cac()
3+
4+
cli
5+
.command('something', 'Do something')
6+
.alias('!')
7+
.action(() => {
8+
console.info('Did something!')
9+
})
10+
11+
cli.parse()

examples/default-command.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { cac } from '../src/index.ts'
2+
const cli = cac()
3+
4+
cli
5+
.command('', 'Do something')
6+
.alias('something')
7+
.action(() => {
8+
console.info('Did something!')
9+
})
10+
11+
cli.parse()

src/cac.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class CAC extends EventTarget {
217217
if (shouldParse) {
218218
// Search the default command
219219
for (const command of this.commands) {
220-
if (command.name === '') {
220+
if (command.isDefaultCommand) {
221221
shouldParse = false
222222
const parsed = this.mri(argv.slice(2), command)
223223
this.setParsedInfo(parsed, command)

tests/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,26 @@ describe('--version in help message', () => {
186186
expect(output).toContain(`--version`)
187187
})
188188
})
189+
190+
describe('default commands', () => {
191+
test('simple: empty call', async () => {
192+
const output = await getOutput('default-command.ts', [])
193+
expect(output).toContain('Did something!')
194+
})
195+
196+
test('simple: name alias call', async () => {
197+
const output = await getOutput('default-command.ts', ['something'])
198+
expect(output).toContain('Did something!')
199+
})
200+
201+
// See https://github.com/cacjs/cac/issues/151
202+
test('inverted: empty call', async () => {
203+
const output = await getOutput('default-command-inverted.ts', [])
204+
expect(output).toContain('Did something!')
205+
})
206+
207+
test('inverted: name alias call', async () => {
208+
const output = await getOutput('default-command-inverted.ts', ['something'])
209+
expect(output).toContain('Did something!')
210+
})
211+
})

0 commit comments

Comments
 (0)