UNPKG

3.29 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const chalk_1 = __importDefault(require("chalk"));
7const DEFAULT_SIGNS = [' ', '⚠', '✖'];
8const DEFAULT_COLORS = ['white', 'yellow', 'red'];
9function format(report = {}, options = {}) {
10 const { results = [] } = report;
11 const fi = (result) => formatInput(result, options);
12 const fr = (result) => formatResult(result, options);
13 return results
14 .filter(r => Array.isArray(r.warnings) || Array.isArray(r.errors))
15 .map(result => [...fi(result), ...fr(result)])
16 .reduce((acc, item) => (Array.isArray(item) ? [...acc, ...item] : [...acc, item]), [])
17 .join('\n');
18}
19exports.format = format;
20function formatInput(result, options = {}) {
21 const { color: enabled = true } = options;
22 const { errors = [], warnings = [], input = '' } = result;
23 if (!input) {
24 return [''];
25 }
26 const sign = '⧗';
27 const decoration = enabled ? chalk_1.default.gray(sign) : sign;
28 const commitText = errors.length > 0 ? input : input.split('\n')[0];
29 const decoratedInput = enabled ? chalk_1.default.bold(commitText) : commitText;
30 const hasProblems = errors.length > 0 || warnings.length > 0;
31 return options.verbose || hasProblems
32 ? [`${decoration} input: ${decoratedInput}`]
33 : [];
34}
35function formatResult(result = {}, options = {}) {
36 const { signs = DEFAULT_SIGNS, colors = DEFAULT_COLORS, color: enabled = true } = options;
37 const { errors = [], warnings = [] } = result;
38 const problems = [...errors, ...warnings].map(problem => {
39 const sign = signs[problem.level] || '';
40 const color = colors[problem.level] || 'white';
41 const decoration = enabled ? chalk_1.default[color](sign) : sign;
42 const name = enabled
43 ? chalk_1.default.grey(`[${problem.name}]`)
44 : `[${problem.name}]`;
45 return `${decoration} ${problem.message} ${name}`;
46 });
47 const sign = selectSign(result);
48 const color = selectColor(result);
49 const deco = enabled ? chalk_1.default[color](sign) : sign;
50 const el = errors.length;
51 const wl = warnings.length;
52 const hasProblems = problems.length > 0;
53 const summary = options.verbose || hasProblems
54 ? `${deco} found ${el} problems, ${wl} warnings`
55 : undefined;
56 const fmtSummary = enabled && typeof summary === 'string' ? chalk_1.default.bold(summary) : summary;
57 const help = hasProblems ? `ⓘ Get help: ${options.helpUrl}` : undefined;
58 return [
59 ...problems,
60 hasProblems ? '' : undefined,
61 fmtSummary,
62 help,
63 help ? '' : undefined
64 ].filter((line) => typeof line === 'string');
65}
66exports.formatResult = formatResult;
67exports.default = format;
68function selectSign(result) {
69 if ((result.errors || []).length > 0) {
70 return '✖';
71 }
72 return (result.warnings || []).length ? '⚠' : '✔';
73}
74function selectColor(result) {
75 if ((result.errors || []).length > 0) {
76 return 'red';
77 }
78 return (result.warnings || []).length ? 'yellow' : 'green';
79}
80//# sourceMappingURL=format.js.map
\No newline at end of file