diff --git a/bin/index.js b/bin/index.js index ee669dd..ede01c5 100755 --- a/bin/index.js +++ b/bin/index.js @@ -4,6 +4,7 @@ const chalk = require('chalk'); const program = require('commander'); +const didYouMean = require('didyoumean'); const { version } = require('../package'); @@ -41,11 +42,22 @@ program .description('Shows all commands available') .action(showCommands); +const suggestCommands = cmd => { + const availableCommands = program.commands.map(c => c._name); + + // Get a suggestion from didyoumean.js based on the input. + const suggestion = didYouMean(cmd, availableCommands); + if (suggestion) { + console.log(` ` + chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`)); + } +}; + // Validates any random command fired in program.arguments('').action(cmd => { program.outputHelp(); console.log(` ` + chalk.red(`\n Unknown command ${chalk.yellow(cmd)}.`)); console.log(); + suggestCommands(cmd); }); program.parse(process.argv); diff --git a/package-lock.json b/package-lock.json index 8f7062a..dd77ed4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4761,6 +4761,11 @@ "integrity": "sha1-PvqHMj67hj5mls67AILUj/PW96E=", "dev": true }, + "didyoumean": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/didyoumean/-/didyoumean-1.2.1.tgz", + "integrity": "sha1-6S7f2tplN9SE1zwBcv0eugxJdv8=" + }, "diffie-hellman": { "version": "5.0.3", "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", diff --git a/package.json b/package.json index 205a0b0..ddd0454 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "chalk": "^2.4.1", "cli-table3": "^0.5.1", "commander": "^2.18.0", + "didyoumean": "^1.2.1", "execa": "^1.0.0", "inquirer": "^6.2.0", "node-banner": "^1.3.2",