Skip to content

Commit

Permalink
feat: add hasCommand method
Browse files Browse the repository at this point in the history
Fixes: #160
  • Loading branch information
thetutlage committed Jun 19, 2024
1 parent 272ddf6 commit 7bfd20c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ export class Kernel<Command extends AbstractBaseCommand> {
return this
}

/**
* Check if a command or an alias is registered with kernel
*/
hasCommand(commandName: string): boolean {
commandName = this.#aliases.get(commandName) || commandName
return this.#commands.has(commandName)
}

/**
* Get the current state of the kernel.
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/kernel/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,27 @@ test.group('Kernel | find', () => {
await assert.rejects(() => kernel.find('foo'), 'Command "foo" is not defined')
assert.deepEqual(stack, ['finding'])
})

test('check if a command exists', async ({ assert }) => {
const kernel = Kernel.create()

class MakeController extends BaseCommand {
static commandName = 'make:controller'
static aliases: string[] = ['mc']
}

class MakeModel extends BaseCommand {
static commandName = 'make:model'
}

kernel.addLoader(new ListLoader([MakeController, MakeModel]))
kernel.addAlias('controller', 'make:controller')
await kernel.boot()

assert.isTrue(kernel.hasCommand('mc'))
assert.isTrue(kernel.hasCommand('controller'))
assert.isTrue(kernel.hasCommand('make:model'))
assert.isTrue(kernel.hasCommand('make:controller'))
assert.isFalse(kernel.hasCommand('make:view'))
})
})

0 comments on commit 7bfd20c

Please sign in to comment.