Skip to content

Commit 80f5f17

Browse files
author
Pooya Parsa
committed
feat: new options: stream, profile, clear and showCursor. (#3)
1 parent 6ea3632 commit 80f5f17

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,28 @@ Display name
8787
### `color`
8888
- Default: `green`
8989

90-
Display color
90+
Display color (can be hex (`#xxyyzz`) or a web color like `green`)
9191

9292
### `profile`
9393
- Default: `false`
9494

9595
Enable profiler
9696

97+
### `stream`
98+
Default: `process.stdout`
99+
100+
Output stream.
101+
102+
### `showCursor`
103+
Default: `false`
104+
105+
Show the cursor. This can be useful when a CLI accepts input from a user.
106+
107+
### `clear`
108+
- Default: `true`
109+
110+
Auto clear console when compile is finished.
111+
97112
<h2 align="center">Maintainers</h2>
98113

99114
<table>

src/index.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ import { BULLET, parseRequst, formatRequest, renderBar, printStats, colorize } f
77

88
const sharedState = {};
99

10-
const defaults = { name: 'webpack', color: 'green', profile: false };
10+
const defaults = {
11+
name: 'webpack',
12+
color: 'green',
13+
stream: process.stdout,
14+
profile: false,
15+
clear: false,
16+
showCursor: false,
17+
};
1118

1219
export default class WebpackBarPlugin extends webpack.ProgressPlugin {
1320
constructor(options) {
@@ -22,7 +29,9 @@ export default class WebpackBarPlugin extends webpack.ProgressPlugin {
2229
this.handler = _.throttle(this.handler, 25, { leading: true, trailing: true });
2330
}
2431

25-
this.logUpdate = this.options.logUpdate || logUpdate;
32+
this.logUpdate = this.options.logUpdate || logUpdate.create(this.options.stream, {
33+
showCursor: this.options.showCursor,
34+
});
2635

2736
if (!sharedState[this.options.name]) {
2837
sharedState[this.options.name] = {
@@ -45,7 +54,9 @@ export default class WebpackBarPlugin extends webpack.ProgressPlugin {
4554
}
4655

4756
done() {
48-
logUpdate.clear();
57+
if (this.options.clear) {
58+
logUpdate.clear();
59+
}
4960

5061
if (this.options.profile) {
5162
const stats = sharedState[this.options.name].profile.getStats();
@@ -79,7 +90,8 @@ export default class WebpackBarPlugin extends webpack.ProgressPlugin {
7990

8091
if (state.isRunning) {
8192
isRunning = true;
82-
} else {
93+
} else if (this.options.clear) {
94+
// Hide finished jobs
8395
return;
8496
}
8597

@@ -88,16 +100,18 @@ export default class WebpackBarPlugin extends webpack.ProgressPlugin {
88100
const lName = lColor(_.startCase(name));
89101
const lBar = renderBar(state.progress, state.color);
90102
const lMsg = _.startCase(state.msg);
91-
const lProgress = `(${state.progress}%)`;
92-
const lDetail1 = chalk.grey(state.details[0] || '');
93-
const lDetail2 = chalk.grey(state.details[1] || '');
94-
const lRequest = formatRequest(state.request);
103+
const lProgress = `(${state.progress || 0}%)`;
104+
const lDetail1 = chalk.grey((state.details && state.details[0]) || '');
105+
const lDetail2 = chalk.grey((state.details && state.details[1]) || '');
106+
const lRequest = state.request ? formatRequest(state.request) : '';
95107

96108
lines.push(`${[lIcon, lName, lBar, lMsg, lProgress, lDetail1, lDetail2].join(' ')}\n ${lRequest}`);
97109
});
98110

99111
if (!isRunning) {
100-
this.logUpdate.clear();
112+
if (this.options.clear) {
113+
this.logUpdate.clear();
114+
}
101115
} else {
102116
const title = ` ${chalk.bgBlue.black(' BUILDING ')}`;
103117
this.logUpdate(`\n${title}\n\n${lines.join('\n\n')}`);

0 commit comments

Comments
 (0)