feat(cli): suggest matching commands if the user mistypes (#3860)

(cherry picked from commit ab18cd5952)
This commit is contained in:
James George
2019-05-13 21:06:17 +05:30
committed by Haoqun Jiang
parent fc0fe3a198
commit a893e59282
3 changed files with 5835 additions and 0 deletions

View File

@@ -6,6 +6,10 @@
const chalk = require('chalk')
const semver = require('semver')
const requiredVersion = require('../package.json').engines.node
const didYouMean = require('didyoumean')
// Setting edit distance to 60% of the input string's length
didYouMean.threshold = 0.6
function checkNodeVersion (wanted, id) {
if (!semver.satisfies(process.version, wanted)) {
@@ -196,6 +200,7 @@ program
program.outputHelp()
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
console.log()
suggestCommands(cmd)
})
// add some useful info on help
@@ -230,6 +235,17 @@ if (!process.argv.slice(2).length) {
program.outputHelp()
}
function suggestCommands (cmd) {
const availableCommands = program.commands.map(cmd => {
return cmd._name
})
const suggestion = didYouMean(cmd, availableCommands)
if (suggestion) {
console.log(` ` + chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`))
}
}
function camelize (str) {
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
}

5818
packages/@vue/cli/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -33,6 +33,7 @@
"commander": "^2.20.0",
"debug": "^4.1.0",
"deepmerge": "^3.2.0",
"didyoumean": "^1.2.1",
"download-git-repo": "^1.0.2",
"ejs": "^2.6.1",
"envinfo": "^7.1.0",