frontity
Version:
Frontity cli and entry point to other packages
32 lines (31 loc) • 1.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const leven_1 = __importDefault(require("leven"));
const chalk_1 = __importDefault(require("chalk"));
/**
* The CLI command that runs when the user types a command that doesn't exit.
*
* @param command - Unknown command.
* @param program - Commander instance.
*/
const unknown = (command, program) => {
console.log(chalk_1.default.red(`Unknown command: ${chalk_1.default.bold(command)}`));
const availableCommands = program.commands.map((c) => c._name);
const [suggestion] = availableCommands
// Compute the distance of each command and return it along with the name.
.map((name) => ({ name, distance: (0, leven_1.default)(name, command) }))
// Filter commands by threshold.
.filter(({ name, distance }) => distance < name.length * 0.4)
// Sort them so the one with the least distance would be the first one.
.sort((c1, c2) => c1.distance - c2.distance)
// Map them and return only the command name.
.map((c) => c.name);
if (suggestion) {
console.log(chalk_1.default.cyan(`Did you mean '${suggestion}'?`));
}
program.help();
};
exports.default = unknown;