UNPKG

2.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const color_1 = require("@heroku-cli/color");
5const cli_ux_1 = require("cli-ux");
6const base_1 = require("./commands/base");
7const deps_1 = require("./deps");
8class NotFound extends base_1.default {
9 run() {
10 return tslib_1.__awaiter(this, void 0, void 0, function* () {
11 this.cm = new deps_1.default.CommandManager(this.config);
12 let closest;
13 let binHelp = `${this.config.bin} help`;
14 let id = this.argv[0];
15 let idSplit = id.split(':');
16 if (yield this.cm.findTopic(idSplit[0])) {
17 // if valid topic, update binHelp with topic
18 binHelp = `${binHelp} ${idSplit[0]}`;
19 // if topic:COMMAND present, try closest for id
20 if (idSplit[1])
21 closest = this.closest(id);
22 }
23 else {
24 closest = this.closest(id);
25 }
26 let perhaps = closest ? `Perhaps you meant ${color_1.color.yellow(yield closest)}\n` : '';
27 cli_ux_1.default.error(`${color_1.color.yellow(id)} is not a ${this.config.bin} command.
28${perhaps}Run ${color_1.color.cmd(binHelp)} for a list of available commands.`, { exitCode: 127 });
29 });
30 }
31 allCommands() {
32 return tslib_1.__awaiter(this, void 0, void 0, function* () {
33 let commands = yield this.cm.commands();
34 return commands.map(c => c.id);
35 // TODO add aliases
36 // return this.commandManager.listCommandIDs().reduce((commands, c) => {
37 // return commands.concat([c.id]).concat(c.aliases || [])
38 // }, [])
39 });
40 }
41 closest(cmd) {
42 return tslib_1.__awaiter(this, void 0, void 0, function* () {
43 const DCE = require('string-similarity');
44 return DCE.findBestMatch(cmd, yield this.allCommands()).bestMatch.target;
45 });
46 }
47}
48NotFound.variableArgs = true;
49exports.default = NotFound;