Skip to content

Commit 009a037

Browse files
committed
feat(kernel): add support for command suggestions
1 parent 83877b5 commit 009a037

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

packages/ace/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ace/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"license": "MIT",
3434
"dependencies": {
35+
"fast-levenshtein": "^2.0.6",
3536
"getopts": "^2.2.4",
3637
"kleur": "^3.0.3",
3738
"pad-right": "^0.2.2"

packages/ace/src/Kernel/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export class Kernel {
4343
return this
4444
}
4545

46+
/**
47+
* Returns an array of command names suggestions for a given name.
48+
*/
49+
public getSuggestions (name: string, distance = 3): string[] {
50+
const levenshtein = require('fast-levenshtein')
51+
return Object.keys(this.commands).filter((commandName) => levenshtein.get(name, commandName) <= distance)
52+
}
53+
4654
/**
4755
* Register a global flag to be set on any command. The flag callback is
4856
* executed before executing the registered command.

packages/ace/test/kernel.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ test.group('Kernel | register', () => {
3232
assert.throw(fn, 'Required argument {age} cannot come after optional argument {name}')
3333
})
3434

35-
test('return null when unable to find command', (assert) => {
35+
test('return command suggestions for a given string', (assert) => {
3636
const kernel = new Kernel()
37-
assert.isNull(kernel.find(['greet']))
37+
38+
class Install extends BaseCommand {
39+
public static commandName = 'install'
40+
}
41+
42+
class Greet extends BaseCommand {
43+
public static commandName = 'greet'
44+
}
45+
46+
kernel.register([Install, Greet])
47+
assert.deepEqual(kernel.getSuggestions('itall'), ['install'])
3848
})
3949
})
4050

0 commit comments

Comments
 (0)