UNPKG

2.03 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.applyPrettyPrintOptions = void 0;
4const wrap = require("wrap-ansi");
5const indent = require("indent-string");
6const screen = require("../screen");
7const config_1 = require("../config");
8function applyPrettyPrintOptions(error, options) {
9 const prettyErrorKeys = ['message', 'code', 'ref', 'suggestions'];
10 prettyErrorKeys.forEach(key => {
11 const applyOptionsKey = !(key in error) && options[key];
12 if (applyOptionsKey) {
13 error[key] = options[key];
14 }
15 });
16 return error;
17}
18exports.applyPrettyPrintOptions = applyPrettyPrintOptions;
19const formatSuggestions = (suggestions) => {
20 const label = 'Try this:';
21 if (!suggestions || suggestions.length === 0)
22 return undefined;
23 if (suggestions.length === 1)
24 return `${label} ${suggestions[0]}`;
25 const multiple = suggestions.map(suggestion => `* ${suggestion}`).join('\n');
26 return `${label}\n${indent(multiple, 2)}`;
27};
28function prettyPrint(error) {
29 if (config_1.config.debug) {
30 return error.stack;
31 }
32 const { message, code, suggestions, ref, name: errorSuffix, bang } = error;
33 // errorSuffix is pulled from the 'name' property on CLIError
34 // and is like either Error or Warning
35 const formattedHeader = message ? `${errorSuffix || 'Error'}: ${message}` : undefined;
36 const formattedCode = code ? `Code: ${code}` : undefined;
37 const formattedSuggestions = formatSuggestions(suggestions);
38 const formattedReference = ref ? `Reference: ${ref}` : undefined;
39 const formatted = [formattedHeader, formattedCode, formattedSuggestions, formattedReference]
40 .filter(Boolean)
41 .join('\n');
42 let output = wrap(formatted, screen.errtermwidth - 6, { trim: false, hard: true });
43 output = indent(output, 3);
44 output = indent(output, 1, { indent: bang || '', includeEmptyLines: true });
45 output = indent(output, 1);
46 return output;
47}
48exports.default = prettyPrint;