Skip to content

Commit d29ca42

Browse files
saintsebastiankumar303
authored andcommitted
feat: web-ext run now shows a desktop notification if auto-reloading results in an error (#674)
1 parent 547b639 commit d29ca42

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"minimatch": "3.0.3",
5959
"mz": "2.6.0",
6060
"node-firefox-connect": "1.2.0",
61+
"node-notifier": "4.6.1",
6162
"parse-json": "2.2.0",
6263
"regenerator-runtime": "0.10.1",
6364
"sign-addon": "0.2.0",

src/cmd/run.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import type FirefoxProfile from 'firefox-profile';
33
import type Watchpack from 'watchpack';
44

5+
import {
6+
showDesktopNotification as defaultDesktopNotifications,
7+
} from '../util/desktop-notifier';
58
import * as defaultFirefoxApp from '../firefox';
69
import defaultFirefoxConnector from '../firefox/remote';
710
import {
@@ -34,6 +37,7 @@ export type WatcherCreatorParams = {
3437
sourceDir: string,
3538
artifactsDir: string,
3639
onSourceChange?: OnSourceChangeFn,
40+
desktopNotifications?: typeof defaultDesktopNotifications,
3741
};
3842

3943
export type WatcherCreatorFn = (params: WatcherCreatorParams) => Watchpack;
@@ -42,17 +46,23 @@ export function defaultWatcherCreator(
4246
{
4347
addonId, client, sourceDir, artifactsDir,
4448
onSourceChange = defaultSourceWatcher,
49+
desktopNotifications = defaultDesktopNotifications,
4550
}: WatcherCreatorParams
4651
): Watchpack {
4752
return onSourceChange({
4853
sourceDir,
4954
artifactsDir,
5055
onChange: () => {
5156
log.debug(`Reloading add-on ID ${addonId}`);
57+
5258
return client.reloadAddon(addonId)
5359
.catch((error) => {
5460
log.error('\n');
5561
log.error(error.stack);
62+
desktopNotifications({
63+
title: 'web-ext run: error occured',
64+
message: error.message,
65+
});
5666
throw error;
5767
});
5868
},

src/util/desktop-notifier.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* @flow */
2+
import defaultNotifier from 'node-notifier';
3+
4+
import {createLogger} from './logger';
5+
import type {Logger} from './logger';
6+
7+
const defaultLog = createLogger(__filename);
8+
9+
type desktopNotificationsParams = {
10+
title: string,
11+
message: string,
12+
icon?: string,
13+
};
14+
15+
export type desktopNotificationsOptions = {
16+
notifier?: typeof defaultNotifier,
17+
log?: Logger,
18+
};
19+
20+
export function showDesktopNotification(
21+
{
22+
title, message, icon,
23+
}: desktopNotificationsParams,
24+
{
25+
notifier = defaultNotifier,
26+
log = defaultLog,
27+
}: desktopNotificationsOptions = {}
28+
): Promise<void> {
29+
30+
return new Promise((resolve, reject) => {
31+
notifier.notify({title, message, icon}, (err, res) => {
32+
if (err) {
33+
log.debug(`Desktop notifier error: ${err.message},` +
34+
` response: ${res}`);
35+
reject(err);
36+
} else {
37+
resolve();
38+
}
39+
});
40+
});
41+
}

tests/unit/test-cmd/test.run.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ describe('run', () => {
280280
sourceDir: '/path/to/extension/source/',
281281
artifactsDir: '/path/to/web-ext-artifacts',
282282
onSourceChange: sinon.spy(() => {}),
283+
desktopNotifications: sinon.spy(() => Promise.resolve()),
283284
};
284285
return {
285286
config,
@@ -322,6 +323,24 @@ describe('run', () => {
322323
});
323324
});
324325

326+
it('notifies user on error from source change handler', () => {
327+
const {config, createWatcher} = prepare();
328+
config.client.reloadAddon = () => Promise.reject(new Error('an error'));
329+
createWatcher();
330+
331+
assert.equal(config.onSourceChange.called, true);
332+
// Simulate executing the handler when a source file changes.
333+
return config.onSourceChange.firstCall.args[0].onChange()
334+
.then(makeSureItFails())
335+
.catch((error) => {
336+
assert.equal(config.desktopNotifications.called, true);
337+
assert.equal(
338+
config.desktopNotifications.lastCall.args[0].message,
339+
error.message
340+
);
341+
});
342+
});
343+
325344
it('throws errors from source change handler', () => {
326345
const {createWatcher, config} = prepare();
327346
config.client.reloadAddon = () => Promise.reject(new Error('an error'));
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* @flow */
2+
import {it, describe} from 'mocha';
3+
import {assert} from 'chai';
4+
import sinon from 'sinon';
5+
6+
import {showDesktopNotification} from '../../../src/util/desktop-notifier';
7+
import {createLogger} from '../../../src/util/logger';
8+
import {makeSureItFails} from '../helpers';
9+
10+
describe('util/desktop-notifier', () => {
11+
describe('desktopNotifications()', () => {
12+
const expectedNotification = {
13+
title: 'web-ext run: title',
14+
message: 'message',
15+
};
16+
17+
it('is called and creates a message with correct parameters', () => {
18+
const fakeNotifier = {
19+
notify: sinon.spy((options, callback) => callback()),
20+
};
21+
return showDesktopNotification(expectedNotification, {
22+
notifier: fakeNotifier,
23+
})
24+
.then(() => {
25+
assert.ok(fakeNotifier.notify.called);
26+
assert.equal(
27+
fakeNotifier.notify.firstCall.args[0].title,
28+
'web-ext run: title',
29+
);
30+
assert.equal(fakeNotifier.notify.firstCall.args[0].message,
31+
'message');
32+
});
33+
});
34+
35+
it('logs error when notifier fails', () => {
36+
const expectedError = new Error('an error');
37+
const fakeLog = createLogger(__filename);
38+
sinon.spy(fakeLog, 'debug');
39+
const fakeNotifier = {
40+
notify: (obj, callback) => {
41+
callback(expectedError, 'response');
42+
},
43+
};
44+
45+
return showDesktopNotification(expectedNotification, {
46+
notifier: fakeNotifier,
47+
log: fakeLog,
48+
})
49+
.then(makeSureItFails())
50+
.catch(() => {
51+
assert.ok(fakeLog.debug.called);
52+
assert.equal(fakeLog.debug.firstCall.args[0],
53+
`Desktop notifier error: ${expectedError.message}, ` +
54+
'response: response');
55+
});
56+
});
57+
58+
});
59+
});

0 commit comments

Comments
 (0)