-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.ts
41 lines (34 loc) · 1.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {color} from '@heroku-cli/color'
import {Hook} from '@oclif/config'
import {cli} from 'cli-ux'
import * as Levenshtein from 'fast-levenshtein'
import * as _ from 'lodash'
const hook: Hook<'command_not_found'> = async function (opts) {
const commandIDs = opts.config.commandIDs
if (!commandIDs.length) return
function closest(cmd: string) {
return _.minBy(commandIDs, c => Levenshtein.get(cmd, c))!
}
let binHelp = `${opts.config.bin} help`
let idSplit = opts.id.split(':')
if (await opts.config.findTopic(idSplit[0])) {
// if valid topic, update binHelp with topic
binHelp = `${binHelp} ${idSplit[0]}`
}
let suggestion: string = closest(opts.id)
this.warn(`${color.yellow(opts.id)} is not a ${opts.config.bin} command.`)
let response
try {
response = await cli.prompt(`Did you mean ${color.blueBright(suggestion)}? [y/n]`, {timeout: 4900})
} catch (err) {
this.log('')
this.debug(err)
}
if (response === 'y') {
const argv = process.argv
await this.config.runCommand(suggestion, argv.slice(3, argv.length))
this.exit(0)
}
this.error(`Run ${color.cmd(binHelp)} for a list of available commands.`, {exit: 127})
}
export default hook