feat: suggest matching commands

This commit is contained in:
jamesgeorge007 2019-06-03 22:33:59 +05:30
parent e06834b9a1
commit 1b84e233dd
2 changed files with 19 additions and 1 deletions

View file

@ -39,6 +39,7 @@
"cheerio": "^1.0.0-rc.1",
"commander": "^2.8.1",
"denodeify": "^1.2.1",
"didyoumean": "^1.2.1",
"glob": "^7.0.6",
"lodash": "^4.17.10",
"markdown-it": "^8.3.1",

View file

@ -1,4 +1,6 @@
import * as program from 'commander';
const didYouMean = require('didyoumean');
import { packageCommand, ls } from './package';
import { publish, unpublish } from './publish';
import { show } from './show';
@ -51,6 +53,17 @@ function main(task: Promise<any>): void {
});
}
function suggestCommands(cmd: string): void {
const availableCommands = program.commands.map(cmd => {
return cmd._name;
});
const suggestion: string | string[] = didYouMean(cmd, availableCommands);
if (suggestion) {
log.warn(`Did you mean ${suggestion}?`);
}
}
module.exports = function (argv: string[]): void {
program
.version(pkg.version);
@ -127,7 +140,11 @@ module.exports = function (argv: string[]): void {
program
.command('*')
.action(() => program.help());
.action((cmd: string) => {
program.outputHelp();
log.error(`Unknown command ${cmd}`);
suggestCommands(cmd);
});
program.parse(argv);