Skip to content

Commit c964696

Browse files
byCedricmarionebl
authored andcommitted
refactor(format): use new report format and output as string
1 parent 3fc89f4 commit c964696

File tree

3 files changed

+138
-55
lines changed

3 files changed

+138
-55
lines changed

@commitlint/format/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"concurrently": "3.5.1",
6969
"cross-env": "5.1.1",
7070
"lodash.includes": "4.3.0",
71+
"lodash.merge": "4.3.0",
7172
"rimraf": "2.6.1",
7273
"xo": "0.20.3"
7374
},

@commitlint/format/src/index.js

+43-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,50 @@
11
import chalk from 'chalk';
2+
import merge from 'lodash.merge';
23

34
const DEFAULT_SIGNS = [' ', '⚠', '✖'];
45
const DEFAULT_COLORS = ['white', 'yellow', 'red'];
56

6-
export default function format(report = {}, options = {}) {
7+
export default function format(report = {}, flags = {}, options = {}) {
8+
const opts = merge({color: flags.color}, options);
9+
const {results = []} = report;
10+
11+
if (results.length > 0) {
12+
return results
13+
.map(
14+
result =>
15+
`${formatInput(result, opts)}${formatResult(result, opts).join('\n')}`
16+
)
17+
.join('\n');
18+
}
19+
20+
// Output a summary when nothing is found
21+
return formatResult({}, opts).join('\n');
22+
}
23+
24+
function formatInput(result = {}, options = {}) {
25+
const {color: enabled = true} = options;
26+
const {errors = [], input = ''} = result;
27+
28+
if (!input) {
29+
return '';
30+
}
31+
32+
const sign = '⧗';
33+
const decoration = enabled ? chalk.gray(sign) : sign;
34+
const commitText = errors.length > 0 ? `\n${input}\n` : input.split('\n')[0];
35+
36+
const decoratedInput = enabled ? chalk.bold(commitText) : commitText;
37+
38+
return `\n${decoration} input: ${decoratedInput}\n`;
39+
}
40+
41+
function formatResult(result = {}, options = {}) {
742
const {
843
signs = DEFAULT_SIGNS,
944
colors = DEFAULT_COLORS,
1045
color: enabled = true
1146
} = options;
12-
const {errors = [], warnings = []} = report;
47+
const {errors = [], warnings = []} = result;
1348

1449
const problems = [...errors, ...warnings].map(problem => {
1550
const sign = signs[problem.level] || '';
@@ -31,16 +66,16 @@ export default function format(report = {}, options = {}) {
3166
return [...problems, enabled ? chalk.bold(summary) : summary];
3267
}
3368

34-
function selectSign(report) {
35-
if (report.errors.length > 0) {
69+
function selectSign(result) {
70+
if (result.errors.length > 0) {
3671
return '✖';
3772
}
38-
return report.warnings.length ? '⚠' : '✔';
73+
return result.warnings.length ? '⚠' : '✔';
3974
}
4075

41-
function selectColor(report) {
42-
if (report.errors.length > 0) {
76+
function selectColor(result) {
77+
if (result.errors.length > 0) {
4378
return 'red';
4479
}
45-
return report.warnings.length ? 'yellow' : 'green';
80+
return result.warnings.length ? 'yellow' : 'green';
4681
}

@commitlint/format/src/index.test.js

+94-47
Original file line numberDiff line numberDiff line change
@@ -7,91 +7,138 @@ const ok = chalk.bold(`${chalk.green('✔')} found 0 problems, 0 warnings`);
77

88
test('does nothing without arguments', t => {
99
const actual = format();
10-
t.deepEqual(actual, [ok]);
10+
t.deepEqual(actual, ok);
11+
});
12+
13+
test('does nothing without report results', t => {
14+
const actual = format({results: []});
15+
t.deepEqual(actual, ok);
1116
});
1217

1318
test('does nothing without .errors and .warnings', t => {
14-
const actual = format({});
15-
t.deepEqual(actual, [ok]);
19+
const actual = format({results: [{}]});
20+
t.deepEqual(actual, ok);
1621
});
1722

1823
test('returns empty summary of problems for empty .errors and .warnings', t => {
19-
const [msg] = format({
20-
errors: [],
21-
warnings: []
24+
const actual = format({
25+
results: [
26+
{
27+
errors: [],
28+
warnings: []
29+
}
30+
]
2231
});
2332

24-
t.true(msg.includes('0 problems, 0 warnings'));
33+
t.true(actual.includes('0 problems, 0 warnings'));
2534
});
2635

2736
test('returns a correct of empty .errors and .warnings', t => {
28-
const [err, prob, msg] = format({
29-
errors: [
37+
const actualError = format({
38+
results: [
3039
{
31-
level: 2,
32-
name: 'error-name',
33-
message: 'There was an error'
40+
errors: [
41+
{
42+
level: 2,
43+
name: 'error-name',
44+
message: 'There was an error'
45+
}
46+
]
3447
}
35-
],
36-
warnings: [
48+
]
49+
});
50+
51+
const actualWarning = format({
52+
results: [
3753
{
38-
level: 1,
39-
name: 'warning-name',
40-
message: 'There was a problem'
54+
warnings: [
55+
{
56+
level: 1,
57+
name: 'warning-name',
58+
message: 'There was a problem'
59+
}
60+
]
4161
}
4262
]
4363
});
4464

45-
t.true(includes(err, 'There was an error'));
46-
t.true(includes(prob, 'There was a problem'));
47-
t.true(includes(msg, '1 problems, 1 warnings'));
65+
t.true(includes(actualError, 'There was an error'));
66+
t.true(includes(actualError, '1 problems, 0 warnings'));
67+
t.true(includes(actualWarning, 'There was a problem'));
68+
t.true(includes(actualWarning, '0 problems, 1 warnings'));
4869
});
4970

5071
test('uses appropriate signs by default', t => {
51-
const [err, warn] = format({
52-
errors: [
72+
const actualError = format({
73+
results: [
5374
{
54-
level: 2,
55-
name: 'error-name',
56-
message: 'There was an error'
75+
errors: [
76+
{
77+
level: 2,
78+
name: 'error-name',
79+
message: 'There was an error'
80+
}
81+
]
5782
}
58-
],
59-
warnings: [
83+
]
84+
});
85+
86+
const actualWarning = format({
87+
results: [
6088
{
61-
level: 1,
62-
name: 'warning-name',
63-
message: 'There was a problem'
89+
warnings: [
90+
{
91+
level: 1,
92+
name: 'warning-name',
93+
message: 'There was a problem'
94+
}
95+
]
6496
}
6597
]
6698
});
6799

68-
t.true(includes(err, '✖'));
69-
t.true(includes(warn, '⚠'));
100+
t.true(includes(actualError, '✖'));
101+
t.true(includes(actualWarning, '⚠'));
70102
});
71103

72104
test('uses signs as configured', t => {
73-
const [err, warn] = format(
105+
const options = {signs: ['HNT', 'WRN', 'ERR']};
106+
const actualError = format(
74107
{
75-
errors: [
108+
results: [
76109
{
77-
level: 2,
78-
name: 'error-name',
79-
message: 'There was an error'
110+
errors: [
111+
{
112+
level: 2,
113+
name: 'error-name',
114+
message: 'There was an error'
115+
}
116+
]
80117
}
81-
],
82-
warnings: [
118+
]
119+
},
120+
{},
121+
options
122+
);
123+
124+
const actualWarning = format(
125+
{
126+
results: [
83127
{
84-
level: 1,
85-
name: 'warning-name',
86-
message: 'There was a problem'
128+
warnings: [
129+
{
130+
level: 1,
131+
name: 'warning-name',
132+
message: 'There was a problem'
133+
}
134+
]
87135
}
88136
]
89137
},
90-
{
91-
signs: ['HNT', 'WRN', 'ERR']
92-
}
138+
{},
139+
options
93140
);
94141

95-
t.true(includes(err, 'ERR'));
96-
t.true(includes(warn, 'WRN'));
142+
t.true(includes(actualError, 'ERR'));
143+
t.true(includes(actualWarning, 'WRN'));
97144
});

0 commit comments

Comments
 (0)