Skip to content

Commit e852855

Browse files
feat(arguments): Suggest matching commands if the user makes a typo 🚧
1 parent 89e0697 commit e852855

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-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)) {
@@ -193,6 +197,7 @@ program
193197
program
194198
.arguments('<command>')
195199
.action((cmd) => {
200+
suggestCommands(cmd)
196201
program.outputHelp()
197202
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
198203
console.log()
@@ -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.cyan(`Warning: ${chalk.yellow(`Did you mean ${suggestion} ?`)}`))
246+
}
247+
}
248+
233249
function camelize (str) {
234250
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
235251
}

packages/@vue/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"commander": "^2.20.0",
3434
"debug": "^4.1.0",
3535
"deepmerge": "^3.2.0",
36+
"didyoumean": "^1.2.1",
3637
"download-git-repo": "^1.0.2",
3738
"ejs": "^2.6.1",
3839
"envinfo": "^7.1.0",

0 commit comments

Comments
 (0)