Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
feat: supports the default command fallback when command not found (r…
Browse files Browse the repository at this point in the history
…esolved #277)
  • Loading branch information
snowyu committed Jul 8, 2020
1 parent db13f7e commit a3a7850
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ export class Config implements IConfig {

async runCommand(id: string, argv: string[] = []) {
debug('runCommand %s %o', id, argv)
const c = this.findCommand(id)
let c = this.findCommand(id)
if (!c) {
c = this.findCommand('default')
argv.unshift(id)
}
if (!c) {
await this.runHook('command_not_found', {id})
throw new CLIError(`command ${id} not found`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class Command {
static run() {
console.log('it works!')
}

static aliases = ['default']
}
2 changes: 2 additions & 0 deletions test/fixtures/typescript/src/hooks/postrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ export default function postrun(options: any) {
console.log('running ts postrun hook')
if (options.Command.id === 'foo:bar:test-result') {
console.log(options.result)
} else if (options.Command.id === 'foo:bar:default-result') {
console.log(options.argv)
}
}
7 changes: 7 additions & 0 deletions test/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ describe('typescript', () => {
await (ctx.config.runHook as any)('init', {id: 'myid', argv: ['foo']})
expect(ctx.stdout).to.equal('running ts init hook\n')
})

withConfig
.stdout()
.it('runs ts default command if command not found', async ctx => {
await ctx.config.runCommand('no-such-command')
expect(ctx.stdout).to.equal('running ts prerun hook\nit works!\nrunning ts postrun hook\n[ \'no-such-command\' ]\n')
})
})

0 comments on commit a3a7850

Please sign in to comment.