Skip to content
Permalink
Browse files

feat: expose the internal notifier options

BREAKING CHANGE: the return of the startNotifying is now an object
  • Loading branch information...
satazor committed Dec 15, 2017
1 parent 1b1edb8 commit 102b7f53d92cc47037aca92b73eecfe5f5361c38
Showing with 67 additions and 36 deletions.
  1. +3 −2 README.md
  2. +4 −1 index.js
  3. +9 −3 package-lock.json
  4. +2 −1 package.json
  5. +9 −0 test/__snapshots__/notifier.spec.js.snap
  6. +40 −29 test/{index.spec.js → notifier.spec.js}
@@ -29,10 +29,11 @@ Notify webpack compilation status to your operating system when using [webpack-s
```js
const startNotifying = require('webpack-sane-compiler-notifier');
const stopNotifying = startNotifying(compiler, {/* options */});
const { stop, options } = startNotifying(compiler, {/* options */});
// Call compiler.run() or compiler.watch() to start a compilation and you will see OS notifications showing up
// Additionally, calling stopNotifying() will stop listening to the compiler events
// Calling stop() will stop listening to the compiler events
// Furthermore, you have access to the options that were computed by the merge of provided options and the defaults
```

### Available options:
@@ -69,7 +69,10 @@ function startNotifying(compiler, options) {
.on('error', onError)
.on('end', onEnd);

return stopNotifying;
return {
stop: stopNotifying,
options,
};
}

module.exports = startNotifying;

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -55,13 +55,14 @@
"eslint-config-moxy": "^4.1.0",
"husky": "^0.14.3",
"jest": "^21.2.1",
"jest-serializer-path": "^0.1.5",
"lint-staged": "^6.0.0",
"standard-version": "^4.2.0"
},
"dependencies": {
"node-notifier": "^5.1.2",
"read-pkg-up": "^3.0.0",
"strip-ansi": "^4.0.0",
"webpack-sane-compiler-reporter": "^1.0.0"
"webpack-sane-compiler-reporter": "^2.0.0"
}
}
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should provide the internal options 1`] = `
Object {
"icon": "<PROJECT_ROOT>/webpack-logo.png",
"sound": false,
"title": "foo",
}
`;
@@ -4,8 +4,11 @@ const path = require('path');
const EventEmitter = require('events');
const readPkgUp = require('read-pkg-up');
const notifier = require('node-notifier');
const serializer = require('jest-serializer-path');
const startNotifying = require('..');

expect.addSnapshotSerializer(serializer);

jest.mock('node-notifier', () => ({
notify: jest.fn(),
}));
@@ -17,11 +20,11 @@ const sound = false;
beforeEach(jest.clearAllMocks);

it('should report a build success', () => {
const saneCompiler = new EventEmitter();
const compiler = new EventEmitter();

startNotifying(saneCompiler);
saneCompiler.emit('begin');
saneCompiler.emit('end');
startNotifying(compiler);
compiler.emit('begin');
compiler.emit('end');

expect(notifier.notify).toHaveBeenCalledTimes(1);
expect(notifier.notify.mock.calls[0][0]).toMatchObject({
@@ -33,11 +36,11 @@ it('should report a build success', () => {
});

it('should report a build error', () => {
const saneCompiler = new EventEmitter();
const compiler = new EventEmitter();

startNotifying(saneCompiler);
saneCompiler.emit('begin');
saneCompiler.emit('error', new Error('foo'));
startNotifying(compiler);
compiler.emit('begin');
compiler.emit('error', new Error('foo'));

expect(notifier.notify).toHaveBeenCalledTimes(1);
expect(notifier.notify.mock.calls[0][0]).toMatchObject({
@@ -49,41 +52,41 @@ it('should report a build error', () => {
});

it('should not report subsequent successful builds', () => {
const saneCompiler = new EventEmitter();
const compiler = new EventEmitter();

startNotifying(saneCompiler);
saneCompiler.emit('begin');
saneCompiler.emit('end');
saneCompiler.emit('begin');
saneCompiler.emit('end');
startNotifying(compiler);
compiler.emit('begin');
compiler.emit('end');
compiler.emit('begin');
compiler.emit('end');

expect(notifier.notify).toHaveBeenCalledTimes(1);
});

it('should report a successful build after a failed one', () => {
const saneCompiler = new EventEmitter();
const compiler = new EventEmitter();

startNotifying(saneCompiler);
saneCompiler.emit('begin');
saneCompiler.emit('error', new Error('foo'));
saneCompiler.emit('begin');
saneCompiler.emit('end');
startNotifying(compiler);
compiler.emit('begin');
compiler.emit('error', new Error('foo'));
compiler.emit('begin');
compiler.emit('end');

expect(notifier.notify).toHaveBeenCalledTimes(2);
expect(notifier.notify.mock.calls[0][0].message).toMatch('foo');
expect(notifier.notify.mock.calls[1][0].message).toMatch('Build successful');
});

it('should allow a custom title, icon and sound options', () => {
const saneCompiler = new EventEmitter();
const compiler = new EventEmitter();

startNotifying(saneCompiler, {
startNotifying(compiler, {
title: 'foo',
icon: 'bar',
sound: true,
});
saneCompiler.emit('begin');
saneCompiler.emit('end');
compiler.emit('begin');
compiler.emit('end');

expect(notifier.notify).toHaveBeenCalledTimes(1);
expect(notifier.notify.mock.calls[0][0]).toMatchObject({
@@ -95,18 +98,26 @@ it('should allow a custom title, icon and sound options', () => {
});

it('should stop notifying if stopNotifying was called', () => {
const saneCompiler = new EventEmitter();
const compiler = new EventEmitter();

const stopNotifying = startNotifying(saneCompiler, {
const { stop } = startNotifying(compiler, {
title: 'foo',
icon: 'bar',
sound: true,
});

stopNotifying();
stop();

saneCompiler.emit('begin');
saneCompiler.emit('end');
compiler.emit('begin');
compiler.emit('end');

expect(notifier.notify).toHaveBeenCalledTimes(0);
});

it('should provide the internal options', () => {
const compiler = new EventEmitter();

const { options } = startNotifying(compiler, { title: 'foo' });

expect(options).toMatchSnapshot();
});

0 comments on commit 102b7f5

Please sign in to comment.
You can’t perform that action at this time.