forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (66 loc) · 2.13 KB
/
index.js
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
import chalk from 'chalk';
const DEFAULT_SIGNS = [' ', '⚠', '✖'];
const DEFAULT_COLORS = ['white', 'yellow', 'red'];
export default function format(report = {}, options = {}) {
const {results = []} = report;
if (results.length > 0) {
return results
.map(
result =>
`${formatInput(result, options)}${formatResult(result, options).join(
'\n'
)}`
)
.join('\n');
}
// Output a summary when nothing is found
return formatResult({}, options).join('\n');
}
function formatInput(result = {}, options = {}) {
const {color: enabled = true} = options;
const {errors = [], input = ''} = result;
if (!input) {
return '';
}
const sign = '⧗';
const decoration = enabled ? chalk.gray(sign) : sign;
const commitText = errors.length > 0 ? `\n${input}\n` : input.split('\n')[0];
const decoratedInput = enabled ? chalk.bold(commitText) : commitText;
return `\n${decoration} input: ${decoratedInput}\n`;
}
function formatResult(result = {}, options = {}) {
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 color = colors[problem.level] || 'white';
const decoration = enabled ? chalk[color](sign) : sign;
const name = enabled
? chalk.grey(`[${problem.name}]`)
: `[${problem.name}]`;
return `${decoration} ${problem.message} ${name}`;
});
const sign = selectSign({errors, warnings});
const color = selectColor({errors, warnings});
const decoration = enabled ? chalk[color](sign) : sign;
const summary = `${decoration} found ${errors.length} problems, ${
warnings.length
} warnings \n (Need help? -> https://github.com/marionebl/commitlint#what-is-commitlint)\n\n`;
return [...problems, enabled ? chalk.bold(summary) : summary];
}
function selectSign(result) {
if (result.errors.length > 0) {
return '✖';
}
return result.warnings.length ? '⚠' : '✔';
}
function selectColor(result) {
if (result.errors.length > 0) {
return 'red';
}
return result.warnings.length ? 'yellow' : 'green';
}