UNPKG

cli-engine

Version:
64 lines (50 loc) 1.77 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotFound = undefined; var _plugins = require('./plugins'); var _plugins2 = _interopRequireDefault(_plugins); var _cliUx = require('cli-ux'); var _color = require('cli-engine-command/lib/color'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class NotFound { constructor(config, argv) { this.argv = argv; this.config = config; this.cli = new _cliUx.CLI({ mock: config.mock }); this.plugins = new _plugins2.default(config); } allCommands() { return this.plugins.commands.reduce((commands, c) => { return commands.concat([c.id]).concat(c.aliases || []); }, []); } closest(cmd) { const DCE = require('string-similarity'); return DCE.findBestMatch(cmd, this.allCommands()).bestMatch.target; } async isValidTopic(name) { let t = await this.plugins.findTopic(name); return !!t; } async run() { await this.plugins.load(); let closest; let binHelp = `${this.config.bin} help`; let id = this.argv[1]; let idSplit = id.split(':'); if (await this.isValidTopic(idSplit[0])) { // if valid topic, update binHelp with topic binHelp = `${binHelp} ${idSplit[0]}`; // if topic:COMMAND present, try closest for id if (idSplit[1]) closest = this.closest(id); } else { closest = this.closest(id); } let perhaps = closest ? `Perhaps you meant ${_color.color.yellow(closest)}\n` : ''; this.cli.error(`${_color.color.yellow(this.argv[1])} is not a ${this.config.bin} command. ${perhaps}Run ${_color.color.cmd(binHelp)} for a list of available topics.`, { exitCode: 127 }); } } exports.NotFound = NotFound;