-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (62 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
const path = require('path');
const os = require('os');
const notifier = require('node-notifier');
const readPkgUp = require('read-pkg-up');
const stripAnsi = require('strip-ansi');
const { renderers } = require('webpack-sane-compiler-reporter');
let defaultTitle;
function getDefaultTitle() {
if (!defaultTitle) {
/* istanbul ignore next */
defaultTitle = readPkgUp.sync().pkg.name || 'Unknown project';
}
return defaultTitle;
}
function createNotifier({ title, icon, sound }) {
// On osx, omit the icon because it will be cut off in the notification center
const omitIcon = os.platform() === 'darwin';
return (message) => notifier.notify({
title,
message,
contentImage: icon,
icon: omitIcon ? undefined : icon,
sound,
});
}
function startNotifying(compiler, options) {
options = {
title: undefined,
icon: path.join(__dirname, 'webpack-logo.png'),
sound: false,
...options,
};
// Read the default title only if not set to avoid doing unnecessary I/O
options.title = options.title || getDefaultTitle();
let lastBuildSucceeded = false;
const notify = createNotifier(options);
const onError = (err) => {
lastBuildSucceeded = false;
const message = stripAnsi(renderers.error(err));
notify(message);
};
const onEnd = () => {
if (!lastBuildSucceeded) {
lastBuildSucceeded = true;
notify('Build successful');
}
};
const stopNotifying = () => {
compiler
.removeListener('end', onEnd)
.removeListener('error', onError);
};
compiler
.on('error', onError)
.on('end', onEnd);
return {
stop: stopNotifying,
options,
};
}
module.exports = startNotifying;