Skip to content

Commit

Permalink
feat: accept done callback as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Mar 28, 2018
1 parent a2c823f commit 30bfd1e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ Show the cursor. This can be useful when a CLI accepts input from a user.

Auto clear console when compile is finished.

### `done`
- Type: `Function(sharedState, ctx)`

A function that will be called when **all** builds are finished.

This function can optionally return `false` as a signal to stop rendering and printing profile stats.

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

<table>
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const defaults = {
clear: true,
showCursor: false,
enabled: process.stdout.isTTY && !isCI,
done: null,
};

export default class WebpackBarPlugin extends webpack.ProgressPlugin {
Expand Down Expand Up @@ -76,6 +77,18 @@ export default class WebpackBarPlugin extends webpack.ProgressPlugin {
return;
}

if (Object.values(sharedState).find((s) => s.isRunning)) {
return;
}

if (typeof this.options.done === 'function') {
const result = this.options.done(sharedState, this);
if (result === false) {
// Special signal to do nothing
return;
}
}

this.render();

if (this.options.profile) {
Expand Down
4 changes: 4 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ describe('webpackbar', () => {
const logUpdate = mockLogUpdate();

test('compile', async () => {
const done = jest.fn();

const compiler = webpack(
basicConfig.from({
name: 'test1',
enabled: true,
profile: true,
color: '#202020',
logUpdate,
done,
})
);

Expand All @@ -31,6 +34,7 @@ describe('webpackbar', () => {

expect(stats.hasErrors()).toBe(false);
expect(stats.hasWarnings()).toBe(false);
expect(done).toHaveBeenCalledTimes(1);
});

test('logUpdate', () => {
Expand Down

0 comments on commit 30bfd1e

Please sign in to comment.