UNPKG

1.77 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.NotFound = undefined;
7
8var _plugins = require('./plugins');
9
10var _plugins2 = _interopRequireDefault(_plugins);
11
12var _cliUx = require('cli-ux');
13
14var _color = require('cli-engine-command/lib/color');
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18class NotFound {
19
20 constructor(config, argv) {
21 this.argv = argv;
22 this.config = config;
23 this.cli = new _cliUx.CLI({ mock: config.mock });
24 this.plugins = new _plugins2.default(config);
25 }
26
27 allCommands() {
28 return this.plugins.commands.reduce((commands, c) => {
29 return commands.concat([c.id]).concat(c.aliases || []);
30 }, []);
31 }
32
33 closest(cmd) {
34 const DCE = require('string-similarity');
35 return DCE.findBestMatch(cmd, this.allCommands()).bestMatch.target;
36 }
37
38 async isValidTopic(name) {
39 let t = await this.plugins.findTopic(name);
40 return !!t;
41 }
42
43 async run() {
44 await this.plugins.load();
45
46 let closest;
47 let binHelp = `${this.config.bin} help`;
48 let id = this.argv[1];
49 let idSplit = id.split(':');
50 if (await this.isValidTopic(idSplit[0])) {
51 // if valid topic, update binHelp with topic
52 binHelp = `${binHelp} ${idSplit[0]}`;
53 // if topic:COMMAND present, try closest for id
54 if (idSplit[1]) closest = this.closest(id);
55 } else {
56 closest = this.closest(id);
57 }
58
59 let perhaps = closest ? `Perhaps you meant ${_color.color.yellow(closest)}\n` : '';
60 this.cli.error(`${_color.color.yellow(this.argv[1])} is not a ${this.config.bin} command.
61${perhaps}Run ${_color.color.cmd(binHelp)} for a list of available topics.`, { exitCode: 127 });
62 }
63}
64exports.NotFound = NotFound;
\No newline at end of file