112 lines
4.1 KiB
JavaScript
Executable File
112 lines
4.1 KiB
JavaScript
Executable File
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.errorBox = void 0;
|
|
const chalk_1 = __importDefault(require("chalk"));
|
|
const utils_1 = require("./utils");
|
|
const strip_ansi_1 = __importDefault(require("strip-ansi"));
|
|
/**
|
|
* Get the input as highlighted keywords to show to the user
|
|
* with the error that was generated from parsing the input.
|
|
*
|
|
* @param nodes AST nodes
|
|
* @param error A parsing error
|
|
*/
|
|
function highlight(nodes, error) {
|
|
const strings = [];
|
|
let errorIndex = undefined;
|
|
function foundError() {
|
|
if (errorIndex !== undefined)
|
|
return;
|
|
errorIndex = strip_ansi_1.default(strings.join(' ')).length;
|
|
}
|
|
if (error.nodes.length === 0)
|
|
return;
|
|
nodes.forEach(node => {
|
|
if (error.nodes.includes(node)) {
|
|
foundError();
|
|
return strings.push(chalk_1.default.red(node.raw));
|
|
}
|
|
else {
|
|
if (node.type === 'shortOptions') {
|
|
let failed = false;
|
|
let s = '';
|
|
for (const option of node.options) {
|
|
if (error.nodes.includes(option)) {
|
|
s += chalk_1.default.red(option.raw);
|
|
failed = true;
|
|
}
|
|
else {
|
|
s += chalk_1.default.dim(option.raw);
|
|
}
|
|
}
|
|
const prefix = failed ? chalk_1.default.red(`-`) : chalk_1.default.dim('-');
|
|
if (failed) {
|
|
foundError();
|
|
}
|
|
return strings.push(prefix + s);
|
|
}
|
|
return strings.push(chalk_1.default.dim(node.raw));
|
|
}
|
|
});
|
|
return { colorized: strings.join(' '), errorIndex: errorIndex !== null && errorIndex !== void 0 ? errorIndex : 0 };
|
|
}
|
|
/**
|
|
* An error UI
|
|
*
|
|
* @param breadcrumbs The command breadcrumbs to print with the error
|
|
*/
|
|
function errorBox(nodes, errors, breadcrumbs) {
|
|
let withHighlight = [];
|
|
let errorMessages = [];
|
|
for (const error of errors) {
|
|
const highlighted = highlight(nodes, error);
|
|
withHighlight.push({ message: error.message, highlighted });
|
|
}
|
|
let number = 1;
|
|
const maxNumberWidth = String(withHighlight.length).length;
|
|
errorMessages.push(chalk_1.default.red.bold('error: ') +
|
|
'found ' +
|
|
chalk_1.default.yellow(withHighlight.length) +
|
|
' error' +
|
|
(withHighlight.length > 1 ? 's' : ''));
|
|
errorMessages.push('');
|
|
withHighlight
|
|
.filter(x => x.highlighted)
|
|
.forEach(x => {
|
|
if (!x.highlighted) {
|
|
throw new Error('WELP');
|
|
}
|
|
const pad = ''.padStart(x.highlighted.errorIndex);
|
|
errorMessages.push(` ${x.highlighted.colorized}`);
|
|
for (const [index, line] of utils_1.enumerate(x.message.split('\n'))) {
|
|
const prefix = index === 0 ? chalk_1.default.bold('^') : ' ';
|
|
const msg = chalk_1.default.red(` ${pad} ${prefix} ${line}`);
|
|
errorMessages.push(msg);
|
|
}
|
|
errorMessages.push('');
|
|
number++;
|
|
});
|
|
const withNoHighlight = withHighlight.filter(x => !x.highlighted);
|
|
if (number > 1) {
|
|
if (withNoHighlight.length === 1) {
|
|
errorMessages.push('Along the following error:');
|
|
}
|
|
else if (withNoHighlight.length > 1) {
|
|
errorMessages.push('Along the following errors:');
|
|
}
|
|
}
|
|
withNoHighlight.forEach(({ message }) => {
|
|
const num = chalk_1.default.red.bold(`${utils_1.padNoAnsi(number.toString(), maxNumberWidth, 'start')}.`);
|
|
errorMessages.push(` ${num} ${chalk_1.default.red(message)}`);
|
|
number++;
|
|
});
|
|
const helpCmd = chalk_1.default.yellow(breadcrumbs.join(' ') + ' --help');
|
|
errorMessages.push('');
|
|
errorMessages.push(chalk_1.default.red.bold('hint: ') + `for more information, try '${helpCmd}'`);
|
|
return errorMessages.join('\n');
|
|
}
|
|
exports.errorBox = errorBox;
|
|
//# sourceMappingURL=errorBox.js.map
|