-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
87 lines (69 loc) · 2.04 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
82
83
84
85
86
87
var ProgressBar = require('progress');
var chalk = require('chalk');
var webpack = require('webpack');
module.exports = function ProgressBarPlugin(options) {
options = options || {};
var stream = options.stream || process.stderr;
var enabled = stream && stream.isTTY;
if (!enabled) {
return function () {};
}
var barLeft = chalk.bold('[');
var barRight = chalk.bold(']');
var preamble = chalk.cyan.bold(' build ') + barLeft;
var barFormat = options.format || preamble + ':bar' + barRight + chalk.green.bold(' :percent');
var summary = options.summary !== false;
var summaryContent = options.summaryContent;
var customSummary = options.customSummary;
delete options.format;
delete options.total;
delete options.summary;
delete options.summaryContent;
delete options.customSummary;
var barOptions = Object.assign({
complete: '=',
incomplete: ' ',
width: 20,
total: 100,
clear: true
}, options);
var bar = new ProgressBar(barFormat, barOptions);
var running = false;
var startTime = 0;
var lastPercent = 0;
var terminated = false;
return new webpack.ProgressPlugin(function (percent, msg) {
if (terminated) {
return;
}
if (!running && lastPercent !== 0 && !customSummary) {
stream.write('\n');
}
var newPercent = Math.floor(percent * barOptions.width);
if (lastPercent < percent || newPercent === 0) {
lastPercent = percent;
}
bar.update(percent, {
msg: msg
});
if (!running) {
running = true;
startTime = new Date;
lastPercent = 0;
} else if (percent === 1) {
var now = new Date;
var buildTime = (now - startTime) / 1000 + 's';
bar.terminate();
if (summary) {
stream.write(chalk.green.bold('Build completed in ' + buildTime + '\n\n'));
} else if (summaryContent) {
stream.write(summaryContent + '(' + buildTime + ')\n\n');
}
if (customSummary) {
customSummary(buildTime);
}
running = false;
terminated = true;
}
});
};