Skip to content

Commit

Permalink
feat: new options: stream, profile, clear and showCursor. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Mar 27, 2018
1 parent 6ea3632 commit 80f5f17
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,28 @@ Display name
### `color`
- Default: `green`

Display color
Display color (can be hex (`#xxyyzz`) or a web color like `green`)

### `profile`
- Default: `false`

Enable profiler

### `stream`
Default: `process.stdout`

Output stream.

### `showCursor`
Default: `false`

Show the cursor. This can be useful when a CLI accepts input from a user.

### `clear`
- Default: `true`

Auto clear console when compile is finished.

<h2 align="center">Maintainers</h2>

<table>
Expand Down
32 changes: 23 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import { BULLET, parseRequst, formatRequest, renderBar, printStats, colorize } f

const sharedState = {};

const defaults = { name: 'webpack', color: 'green', profile: false };
const defaults = {
name: 'webpack',
color: 'green',
stream: process.stdout,
profile: false,
clear: false,
showCursor: false,
};

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

this.logUpdate = this.options.logUpdate || logUpdate;
this.logUpdate = this.options.logUpdate || logUpdate.create(this.options.stream, {
showCursor: this.options.showCursor,
});

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

done() {
logUpdate.clear();
if (this.options.clear) {
logUpdate.clear();
}

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

if (state.isRunning) {
isRunning = true;
} else {
} else if (this.options.clear) {
// Hide finished jobs
return;
}

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

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

if (!isRunning) {
this.logUpdate.clear();
if (this.options.clear) {
this.logUpdate.clear();
}
} else {
const title = ` ${chalk.bgBlue.black(' BUILDING ')}`;
this.logUpdate(`\n${title}\n\n${lines.join('\n\n')}`);
Expand Down

0 comments on commit 80f5f17

Please sign in to comment.