Skip to content

Commit f79c50c

Browse files
fix: split suggested command from passed command to get varargs
1 parent 470096b commit f79c50c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
/lib
77
/node_modules
88
/tmp
9+
/.idea
10+
/.vscode

src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ const hook: Hook.CommandNotFound = async function (opts) {
2929

3030
let response = ''
3131
try {
32-
response = await ux.prompt(`Did you mean ${color.blueBright(readableSuggestion)}? [y/n]`, {timeout: 4900})
32+
response = await ux.prompt(`Did you mean ${color.blueBright(readableSuggestion)}? [y/n]`, {timeout: 10_000})
3333
} catch (error) {
3434
this.log('')
3535
this.debug(error)
3636
}
3737

3838
if (response === 'y') {
39-
const argv = opts.argv || process.argv.slice(3, process.argv.length)
39+
// this will split the original command from the suggested replacement, and gather the remaining args as varargs to help with situations like:
40+
// confit set foo-bar -> confit:set:foo-bar -> config:set:foo-bar -> config:set foo-bar
41+
const argv = opts.argv?.length ? opts.argv : opts.id.split(':').slice(suggestion.split(':').length)
4042
return this.config.runCommand(suggestion, argv)
4143
}
4244

test/hooks/not-found.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ describe('command_not_found', () => {
2121
expect(ctx.stdout).to.match(/commands.+?\n.*?help/)
2222
})
2323

24+
test
25+
.stub(ux, 'prompt', yes)
26+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
27+
// @ts-ignore
28+
.stub(process, 'argv', ['username'])
29+
.stdout()
30+
.stderr()
31+
.hook('command_not_found', {id: 'commans get'})
32+
.end('runs hook with suggested command on yes with varargs passed', (ctx: any) => {
33+
expect(ctx.stderr).to.be.contain('Warning: commans get is not a @oclif/plugin-not-found command.\n')
34+
expect(ctx.stdout).to.match(/commands.+?\n.*?help/)
35+
})
36+
2437
test
2538
.stderr()
2639
.stub(ux, 'prompt', yes)

0 commit comments

Comments
 (0)