forked from Turbo87/webpack-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (51 loc) · 1.9 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
var stripANSI = require('strip-ansi');
var path = require('path');
var objectAssign = require('object-assign');
var os = require('os');
var notifier = require('node-notifier');
var DEFAULT_LOGO = path.join(__dirname, 'logo.png');
var WebpackNotifierPlugin = module.exports = function(options) {
this.options = options || {};
this.lastBuildSucceeded = false;
};
WebpackNotifierPlugin.prototype.compileMessage = function(stats) {
var error;
if (stats.hasErrors()) {
error = stats.compilation.errors[0];
} else if (stats.hasWarnings() && !this.options.excludeWarnings) {
error = stats.compilation.warnings[0];
} else if (!this.lastBuildSucceeded || this.options.alwaysNotify) {
this.lastBuildSucceeded = true;
return 'Build successful';
} else {
return;
}
this.lastBuildSucceeded = false;
var message;
if (error.module && error.module.rawRequest)
message = error.module.rawRequest + '\n';
if (error.error)
message = 'Error: ' + message + error.error.toString();
else if (error.warning)
message = 'Warning: ' + message + error.warning.toString();
else if (error.message) {
message = 'Warning: ' + message + error.message.toString();
}
return stripANSI(message);
};
WebpackNotifierPlugin.prototype.compilationDone = function(stats) {
var msg = this.compileMessage(stats);
if (msg) {
var contentImage = ('contentImage' in this.options) ?
this.options.contentImage : DEFAULT_LOGO;
notifier.notify(objectAssign({
title: 'Webpack',
message: msg,
contentImage: contentImage,
icon: (os.platform() === 'win32' || os.platform() === 'linux') ? contentImage : undefined
}, this.options));
}
};
WebpackNotifierPlugin.prototype.apply = function(compiler) {
compiler.plugin('done', this.compilationDone.bind(this));
};