1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.applyPrettyPrintOptions = void 0;
|
4 | const wrap = require("wrap-ansi");
|
5 | const indent = require("indent-string");
|
6 | const screen = require("../screen");
|
7 | const config_1 = require("../config");
|
8 | function 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 | }
|
18 | exports.applyPrettyPrintOptions = applyPrettyPrintOptions;
|
19 | const 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 | };
|
28 | function prettyPrint(error) {
|
29 | if (config_1.config.debug) {
|
30 | return error.stack;
|
31 | }
|
32 | const { message, code, suggestions, ref, name: errorSuffix, bang } = error;
|
33 |
|
34 |
|
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 | }
|
48 | exports.default = prettyPrint;
|