-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathformat.ts
More file actions
130 lines (109 loc) · 3.33 KB
/
format.ts
File metadata and controls
130 lines (109 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pc from "picocolors";
import {
PicocolorsColor,
FormattableReport,
FormatOptions,
FormattableResult,
WithInput,
} from "@commitlint/types";
const DEFAULT_SIGNS = [" ", "⚠", "✖"] as const;
const DEFAULT_COLORS: readonly [
PicocolorsColor,
PicocolorsColor,
PicocolorsColor,
] = ["white", "yellow", "red"] as const;
export function format(
report: FormattableReport = {},
options: FormatOptions = {},
): string {
const { results = [] } = report;
const fi = (result: FormattableResult & WithInput) =>
formatInput(result, options);
const fr = (result: FormattableResult) => formatResult(result, options);
return results
.filter((r) => Array.isArray(r.warnings) || Array.isArray(r.errors))
.map((result) => [...fi(result), ...fr(result)])
.reduce(
(acc, item) => (Array.isArray(item) ? [...acc, ...item] : [...acc, item]),
[],
)
.join("\n");
}
function formatInput(
result: FormattableResult & WithInput,
options: FormatOptions = {},
): string[] {
const { color: enabled = true, legacyOutput = false } = options;
const { errors = [], warnings = [], input = "" } = result;
if (!input) {
return [""];
}
const sign = "⧗";
const decoration = enabled ? pc.gray(sign) : sign;
const decoratedInput = enabled ? pc.bold(input) : input;
const hasProblems = errors.length > 0 || warnings.length > 0;
if (!(options.verbose || hasProblems)) {
return [];
}
if (legacyOutput) {
// legacy: single line with 'input: ' prefix, no extra newlines or dashes
return [`${decoration} input: ${decoratedInput}`];
}
return [`${decoration} --- input ---\n${decoratedInput}`];
}
export function formatResult(
result: FormattableResult = {},
options: FormatOptions = {},
): string[] {
const {
signs = DEFAULT_SIGNS,
colors = DEFAULT_COLORS,
color: enabled = true,
} = options;
const { errors = [], warnings = [] } = result;
const problems = [...errors, ...warnings].map((problem) => {
const sign = signs[problem.level] || "";
const colorName: PicocolorsColor =
colors[problem.level] || ("white" as const);
const colorFn = pc[colorName];
const decoration = enabled ? colorFn(sign) : sign;
const name = enabled ? pc.gray(`[${problem.name}]`) : `[${problem.name}]`;
return `${decoration} ${problem.message} ${name}`;
});
const sign = selectSign(result);
const colorName = selectColor(result);
const deco = enabled ? pc[colorName](sign) : sign;
const el = errors.length;
const wl = warnings.length;
const hasProblems = problems.length > 0;
const summary =
options.verbose || hasProblems
? `${deco} found ${el} problems, ${wl} warnings`
: undefined;
const fmtSummary =
enabled && typeof summary === "string" ? pc.bold(summary) : summary;
const help =
hasProblems && options.helpUrl
? `ⓘ Get help: ${options.helpUrl}`
: undefined;
return [
...problems,
hasProblems ? "" : undefined,
fmtSummary,
help,
hasProblems ? "" : undefined,
].filter((line): line is string => typeof line === "string");
}
export default format;
function selectSign(result: FormattableResult): string {
if ((result.errors || []).length > 0) {
return "✖";
}
return (result.warnings || []).length ? "⚠" : "✔";
}
function selectColor(result: FormattableResult): PicocolorsColor {
if ((result.errors || []).length > 0) {
return "red";
}
return (result.warnings || []).length ? "yellow" : "green";
}