Skip to content

Commit fcd2b71

Browse files
Use chalk for colour output, in order for better terminal colour support (and automatic detection and disabling)
1 parent cd7fd2c commit fcd2b71

4 files changed

Lines changed: 28 additions & 24 deletions

File tree

colours.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
const greenFg = "\x1b[32m";
2-
const yellowFg = "\x1b[33m";
3-
const redFg = "\x1b[31m";
4-
const blackBg = "\x1b[40m";
5-
const bold = "\x1b[1m";
6-
const end = "\x1b[0m";
1+
const chalk = require("chalk");
72

83
module.exports.fg = (text, time) => {
9-
let colour;
10-
if (time >= 0) colour = greenFg;
11-
if (time > 2000) colour = yellowFg;
12-
if (time > 10000) colour = redFg;
4+
let textModifier = chalk.bold;
5+
if (time > 10000) textModifier = textModifier.red;
6+
else if (time > 2000) textModifier = textModifier.yellow;
7+
else textModifier = textModifier.green;
138

14-
return (colour || "") + bold + text + end;
9+
return textModifier(text);
1510
};
1611

17-
module.exports.bg = text => blackBg + greenFg + bold + text + end;
18-
19-
module.exports.stripColours = text => text.replace(/\x1b\[[0-9]+m/g, "");
12+
module.exports.bg = text => chalk.bgBlack.green.bold(text);

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
const path = require("path");
22
const fs = require("fs");
3+
const chalk = require("chalk");
34
const { WrappedPlugin } = require("./WrappedPlugin");
45
const { getModuleName, getLoaderNames, prependLoader } = require("./utils");
56
const {
67
getHumanOutput,
78
getMiscOutput,
89
getPluginsOutput,
910
getLoadersOutput,
11+
smpTag,
1012
} = require("./output");
11-
const { stripColours } = require("./colours");
1213

1314
const NS = path.dirname(fs.realpathSync(__filename));
1415

@@ -116,15 +117,19 @@ module.exports = class SpeedMeasurePlugin {
116117
compiler.plugin("done", () => {
117118
this.addTimeEvent("misc", "compile", "end", { fillLast: true });
118119

120+
const outputToFile = typeof this.options.outputTarget === "string";
121+
chalk.enabled = !outputToFile;
119122
const output = this.getOutput();
120-
if (typeof this.options.outputTarget === "string") {
121-
const strippedOutput = stripColours(output);
123+
chalk.enabled = true;
124+
if (outputToFile) {
122125
const writeMethod = fs.existsSync(this.options.outputTarget)
123126
? fs.appendFile
124127
: fs.writeFile;
125-
writeMethod(this.options.outputTarget, strippedOutput + "\n", err => {
128+
writeMethod(this.options.outputTarget, output + "\n", err => {
126129
if (err) throw err;
127-
console.log("Outputted timing info to " + this.options.outputTarget);
130+
console.log(
131+
smpTag() + "Outputted timing info to " + this.options.outputTarget
132+
);
128133
});
129134
} else {
130135
const outputFunc = this.options.outputTarget || console.log;

output.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const MS_IN_MINUTE = 60000;
22
const MS_IN_SECOND = 1000;
33

4+
const chalk = require("chalk");
45
const { fg, bg } = require("./colours");
56
const { groupBy, getAverages, getTotalActiveTime } = require("./utils");
67

@@ -30,10 +31,12 @@ const humanTime = (ms, options = {}) => {
3031
return time;
3132
};
3233

34+
const smpTag = () => bg(" SMP ") + " ⏱ ";
35+
module.exports.smpTag = smpTag;
36+
3337
module.exports.getHumanOutput = (outputObj, options = {}) => {
3438
const hT = x => humanTime(x, options);
35-
const smpTag = bg(" SMP ") + " ⏱ ";
36-
let output = "\n\n" + smpTag + "\n";
39+
let output = "\n\n" + smpTag() + "\n";
3740

3841
if (outputObj.misc) {
3942
output +=
@@ -42,22 +45,22 @@ module.exports.getHumanOutput = (outputObj, options = {}) => {
4245
output += "\n\n";
4346
}
4447
if (outputObj.plugins) {
45-
output += smpTag + " Plugins\n";
48+
output += smpTag() + "Plugins\n";
4649
Object.keys(outputObj.plugins)
4750
.sort(
4851
(name1, name2) => outputObj.plugins[name2] - outputObj.plugins[name1]
4952
)
5053
.forEach(pluginName => {
5154
output +=
52-
fg(pluginName) +
55+
chalk.bold(pluginName) +
5356
" took " +
5457
fg(hT(outputObj.plugins[pluginName]), outputObj.plugins[pluginName]);
5558
output += "\n";
5659
});
5760
output += "\n";
5861
}
5962
if (outputObj.loaders) {
60-
output += smpTag + " Loaders\n";
63+
output += smpTag() + "Loaders\n";
6164
outputObj.loaders.build
6265
.sort((obj1, obj2) => obj2.activeTime - obj1.activeTime)
6366
.forEach(loaderObj => {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@
2828
"devDependencies": {
2929
"jest": "^22.3.0",
3030
"prettier": "1.10.2"
31+
},
32+
"dependencies": {
33+
"chalk": "^2.0.1"
3134
}
3235
}

0 commit comments

Comments
 (0)