Skip to content

Commit 3c5258b

Browse files
committed
Fix error colors when displayed on a light theme
1 parent 0facc4d commit 3c5258b

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

index.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
const WebpackConfig = require('./lib/WebpackConfig');
1313
const configGenerator = require('./lib/config-generator');
1414
const validator = require('./lib/config/validator');
15-
const PrettyError = require('pretty-error');
15+
const prettyError = require('./lib/utils/pretty-error');
1616
const logger = require('./lib/logger');
1717
const parseRuntime = require('./lib/config/parse-runtime');
1818
const chalk = require('chalk');
@@ -1289,10 +1289,7 @@ const EncoreProxy = new Proxy(new Encore(), {
12891289
const res = target[prop](...parameters);
12901290
return (res === target) ? EncoreProxy : res;
12911291
} catch (error) {
1292-
// prettifies errors thrown by our library
1293-
const pe = new PrettyError();
1294-
1295-
console.log(pe.render(error));
1292+
prettyError(error);
12961293
process.exit(1); // eslint-disable-line
12971294
}
12981295
};
@@ -1325,10 +1322,11 @@ const EncoreProxy = new Proxy(new Encore(), {
13251322
// Only keep the 2nd line of the stack trace:
13261323
// - First line should be this file (index.js)
13271324
// - Second line should be the Webpack config file
1328-
const pe = new PrettyError();
1329-
pe.skip((traceLine, lineNumber) => lineNumber !== 1);
1330-
const error = new Error(errorMessage);
1331-
console.log(pe.render(error));
1325+
prettyError(
1326+
new Error(errorMessage),
1327+
{ skipTrace: (traceLine, lineNumber) => lineNumber !== 1 }
1328+
);
1329+
13321330
process.exit(1); // eslint-disable-line
13331331
}
13341332

lib/utils/pretty-error.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const PrettyError = require('pretty-error');
13+
14+
/**
15+
* Render a pretty version of the given error.
16+
*
17+
* Supported options:
18+
* * {function} skipTrace
19+
* An optional callback that defines whether
20+
* or not each line of the eventual stacktrace
21+
* should be kept. First argument is the content
22+
* of the line, second argument is the line number.
23+
*
24+
* @param {*} error
25+
* @param {object} options
26+
*
27+
* @returns {void}
28+
*/
29+
module.exports = function(error, options = {}) {
30+
const pe = new PrettyError();
31+
32+
// Use the default terminal's color
33+
// for the error message.
34+
pe.appendStyle({
35+
'pretty-error > header > message': { color: 'none' }
36+
});
37+
38+
// Allow to skip some parts of the
39+
// stacktrace if there is one.
40+
if (options.skipTrace) {
41+
pe.skip(options.skipTrace);
42+
}
43+
44+
console.log(pe.render(error));
45+
};

0 commit comments

Comments
 (0)