Skip to content

Commit ab18cd5

Browse files
jamesgeorge007haoqunjiang
authored andcommitted
feat(cli): suggest matching commands if the user mistypes (#3860)
1 parent 8fe5850 commit ab18cd5

File tree

3 files changed

+5835
-0
lines changed

3 files changed

+5835
-0
lines changed

packages/@vue/cli/bin/vue.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
const chalk = require('chalk')
77
const semver = require('semver')
88
const requiredVersion = require('../package.json').engines.node
9+
const didYouMean = require('didyoumean')
10+
11+
// Setting edit distance to 60% of the input string's length
12+
didYouMean.threshold = 0.6
913

1014
function checkNodeVersion (wanted, id) {
1115
if (!semver.satisfies(process.version, wanted)) {
@@ -196,6 +200,7 @@ program
196200
program.outputHelp()
197201
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
198202
console.log()
203+
suggestCommands(cmd)
199204
})
200205

201206
// add some useful info on help
@@ -230,6 +235,17 @@ if (!process.argv.slice(2).length) {
230235
program.outputHelp()
231236
}
232237

238+
function suggestCommands (cmd) {
239+
const availableCommands = program.commands.map(cmd => {
240+
return cmd._name
241+
})
242+
243+
const suggestion = didYouMean(cmd, availableCommands)
244+
if (suggestion) {
245+
console.log(` ` + chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`))
246+
}
247+
}
248+
233249
function camelize (str) {
234250
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
235251
}

0 commit comments

Comments
 (0)