UNPKG

balena-cli

Version:

The official balena Command Line Interface

35 lines 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lazy_1 = require("../../utils/lazy"); const hook = async function (opts) { var _a, _b, _c, _d; const Levenshtein = await Promise.resolve().then(() => require('fast-levenshtein')); const _ = await Promise.resolve().then(() => require('lodash')); const commandId = (_a = opts.id) !== null && _a !== void 0 ? _a : ''; const command = (_c = (_b = opts.id) === null || _b === void 0 ? void 0 : _b.replace(':', ' ')) !== null && _c !== void 0 ? _c : ''; const commandIDs = [ ...opts.config.commandIDs, ...opts.config.commands.map((c) => c.aliases).flat(), 'version', ]; function closest(cmd) { return _.minBy(commandIDs, (c) => Levenshtein.get(cmd, c)); } const suggestions = []; suggestions.push(closest(commandId).replace(':', ' ')); if ((_d = opts.argv) === null || _d === void 0 ? void 0 : _d[0]) { suggestions.unshift(closest(`${commandId}: + ${opts.argv[0]}`).replace(':', ' ')); } const ux = (0, lazy_1.getCliUx)(); console.error(`${ux.colorize('yellow', command)} is not a recognized balena command.\n`); console.error(`Did you mean: ? `); suggestions.forEach((s) => { console.error(` ${ux.colorize('cyan', ux.colorize('bold', s))}`); }); console.error(`\nRun ${ux.colorize('cyan', ux.colorize('bold', 'balena help -v'))} for a list of available commands,`); console.error(` or ${ux.colorize('cyan', ux.colorize('bold', 'balena help <command>'))} for detailed help on a specific command.`); const COMMAND_NOT_FOUND = 127; process.exit(COMMAND_NOT_FOUND); }; exports.default = hook; //# sourceMappingURL=suggest.js.map