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