Skip to content

Commit 931bf4e

Browse files
committed
Prettier output
1 parent 7e0cc62 commit 931bf4e

File tree

7 files changed

+86
-27
lines changed

7 files changed

+86
-27
lines changed

src/formatters/defaultError.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const chalk = require('chalk');
43
const concat = require('../utils').concat;
4+
const formatTitle = require('../utils/colors').formatTitle;
55

6-
function displayError(index, severity, error) {
7-
const baseError = chalk.red(`${index + 1}) ${severity}`);
6+
function displayError(severity, error) {
7+
const baseError = formatTitle(severity, severity);
88

99
return concat(
1010
`${baseError} ${error.file ? 'in ' + error.file : ''}`,
@@ -26,8 +26,8 @@ function isDefaultError(error) {
2626
function format(errors, type) {
2727
return errors
2828
.filter(isDefaultError)
29-
.reduce((accum, error, i ) => (
30-
accum.concat(displayError(i, type, error))
29+
.reduce((accum, error) => (
30+
accum.concat(displayError(type, error))
3131
), []);
3232
}
3333

src/friendly-errors-plugin.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ class FriendlyErrorsWebpackPlugin {
4444
}
4545

4646
if (hasErrors) {
47-
this.displayErrors(stats.compilation.errors, 'error', 'red');
47+
this.displayErrors(stats.compilation.errors, 'error');
4848
return;
4949
}
5050

5151
if (hasWarnings) {
52-
this.displayErrors(stats.compilation.warnings, 'warning', 'yellow');
52+
this.displayErrors(stats.compilation.warnings, 'warning');
5353
}
5454
});
5555

5656
compiler.plugin('invalid', () => {
5757
this.clearConsole();
58-
output.log(chalk.cyan('Compiling...'));
58+
output.title('info', 'WAIT', 'Compiling...');
5959
});
6060
}
6161

@@ -67,25 +67,27 @@ class FriendlyErrorsWebpackPlugin {
6767

6868
displaySuccess(stats) {
6969
const time = stats.endTime - stats.startTime;
70-
output.log(chalk.green('Compiled successfully in ' + time + 'ms'));
70+
output.title('success', 'DONE', 'Compiled successfully in ' + time + 'ms');
7171

7272
if (this.compilationSuccessMessage) {
73-
output.log(this.compilationSuccessMessage);
73+
output.info(this.compilationSuccessMessage);
7474
}
7575
}
7676

77-
displayErrors(errors, level, color) {
77+
displayErrors(errors, severity) {
7878

7979
const processedErrors = transformErrors(errors, this.transformers);
8080
const nbErrors = processedErrors.length;
81-
displayCompilationMessage(`Failed to compile with ${nbErrors} ${level}s`, color);
81+
82+
const subtitle = severity === 'error' ? `Failed to compile with ${nbErrors} ${severity}s` : `Compiled with ${nbErrors} ${severity}s`;
83+
output.title(severity, severity.toUpperCase(), subtitle);
8284

8385
if (this.onErrors) {
84-
this.onErrors(level, processedErrors);
86+
this.onErrors(severity, processedErrors);
8587
}
8688

8789
const topErrors = getMaxSeverityErrors(processedErrors);
88-
formatErrors(topErrors, this.formatters, level)
90+
formatErrors(topErrors, this.formatters, severity)
8991
.forEach((chunk) => output.log(chunk));
9092
}
9193
}

src/output.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
3+
const colors = require('./utils/colors');
44
const chalk = require('chalk');
55

66
class Debugger {
@@ -32,6 +32,22 @@ class Debugger {
3232
}
3333
}
3434

35+
info (message) {
36+
if (this.enabled) {
37+
const titleFormatted = colors.formatTitle('info', 'I');
38+
this.log(titleFormatted, message);
39+
}
40+
}
41+
42+
title (severity, title, subtitle) {
43+
if (this.enabled) {
44+
const titleFormatted = colors.formatTitle(severity, title);
45+
const subTitleFormatted = colors.formatText(severity, subtitle);
46+
this.log(titleFormatted, subTitleFormatted);
47+
this.log();
48+
}
49+
}
50+
3551
clearConsole () {
3652
if (!this.capturing && this.enabled) {
3753
process.stdout.write('\x1bc');
@@ -59,4 +75,8 @@ class Debugger {
5975
}
6076
}
6177

78+
function capitalizeFirstLetter (string) {
79+
return string.charAt(0).toUpperCase() + string.slice(1);
80+
}
81+
6282
module.exports = new Debugger();

src/utils/colors.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const chalk = require('chalk');
4+
5+
function formatTitle(severity, message) {
6+
return chalk[bgColor(severity)].black('', message, '');
7+
}
8+
9+
function formatText(severity, message) {
10+
return chalk[textColor(severity)](message);
11+
}
12+
13+
function bgColor(severity) {
14+
const color = textColor(severity);
15+
return 'bg'+ capitalizeFirstLetter(color)
16+
}
17+
18+
function textColor(serverity) {
19+
switch (serverity.toLowerCase()) {
20+
case 'success': return 'green';
21+
case 'info': return 'blue';
22+
case 'warning': return 'yellow';
23+
case 'error': return 'red';
24+
default: return 'red';
25+
}
26+
}
27+
28+
function capitalizeFirstLetter(string) {
29+
return string.charAt(0).toUpperCase() + string.slice(1);
30+
}
31+
32+
module.exports = {
33+
bgColor: bgColor,
34+
textColor: textColor,
35+
formatTitle: formatTitle,
36+
formatText: formatText
37+
};

test/integration.spec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ test('integration : module-errors', t => {
1212
});
1313

1414
assert.deepEqual(logs, [
15-
'',
16-
'Failed to compile with 2 errors',
15+
' ERROR Failed to compile with 2 errors',
1716
'',
1817
'These dependencies were not found in node_modules:',
1918
'',
@@ -31,10 +30,9 @@ test('integration : should display eslint warnings', t => {
3130
});
3231

3332
assert.deepEqual(logs, [
33+
' WARNING Compiled with 1 warnings',
3434
'',
35-
'Failed to compile with 1 warnings',
36-
'',
37-
'1) warning in ./fixtures/eslint-warnings/index.js',
35+
' warning in ./fixtures/eslint-warnings/index.js',
3836
'',
3937
`${__dirname}/fixtures/eslint-warnings/index.js
4038
1:7 warning 'unused' is defined but never used no-unused-vars
@@ -55,10 +53,9 @@ test('integration : babel syntax error', t => {
5553
});
5654

5755
assert.deepEqual(logs, [
56+
' ERROR Failed to compile with 1 errors',
5857
'',
59-
'Failed to compile with 1 errors',
60-
'',
61-
'1) error in ./fixtures/babel-syntax/index.js',
58+
' error in ./fixtures/babel-syntax/index.js',
6259
'',
6360
`Module build failed: SyntaxError: Unexpected token (5:11)
6461

test/unit/formatters/defaultError.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const error = { message: 'Error message', file: './src/index.js' };
88

99
test('Formats errors with no type', () => {
1010
expect(noColor(defaultError([error], 'Warning'))).toEqual([
11-
'1) Warning in ./src/index.js',
11+
' Warning in ./src/index.js',
1212
'',
1313
'Error message',
1414
'',

test/unit/plugin/friendlyErrors.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ test('friendlyErrors : capture invalid message', t => {
1919
mockCompiler.emit('invalid');
2020
});
2121

22-
assert.deepEqual(logs,
23-
['Compiling...']
22+
assert.deepEqual(logs, [
23+
' WAIT Compiling...',
24+
''
25+
]
2426
);
2527
});
2628

@@ -32,7 +34,8 @@ test('friendlyErrors : capture compilation without errors', t => {
3234
});
3335

3436
assert.deepEqual(logs, [
35-
'Compiled successfully in 100ms'
37+
' DONE Compiled successfully in 100ms',
38+
''
3639
]);
3740
});
3841

0 commit comments

Comments
 (0)