forked from Turbo87/webpack-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (109 loc) · 3.83 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var stripANSI = require('strip-ansi');
var path = require('path');
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;
this.isFirstBuild = true;
};
function findFirstDFS(compilation, key) {
var match = compilation[key][0];
if (match) {
return match;
}
var children = compilation.children;
for (var i = 0; i < children.length; ++i) {
match = findFirstDFS(children[i], key);
if (match) {
return match;
}
}
}
WebpackNotifierPlugin.prototype.compileEndOptions = function (stats) {
if (this.isFirstBuild) {
this.isFirstBuild = false;
if (this.options.skipFirstNotification) {
return;
}
}
var imageFromOptions = ('contentImage' in this.options)
? this.options.contentImage
: DEFAULT_LOGO;
var successImage = path.resolve(__dirname + '/img/ok.ico');
var warningsImage = path.resolve(__dirname + '/img/warn.ico');
var errorsImage = path.resolve(__dirname + '/img/error.ico');
if (typeof imageFromOptions == 'object') {
successImage = imageFromOptions['success']
warningsImage = imageFromOptions['warning']
errorsImage = imageFromOptions['error']
}
var error;
var contentImage;
var status;
if (stats.hasErrors()) {
error = findFirstDFS(stats.compilation, 'errors');
contentImage = errorsImage;
status = 'error'
} else if (stats.hasWarnings() && !this.options.excludeWarnings && !this.options.onlyOnError) {
error = findFirstDFS(stats.compilation, 'warnings');
contentImage = warningsImage;
status = 'warning'
} else if ((!this.lastBuildSucceeded || this.options.alwaysNotify) && !this.options.onlyOnError) {
this.lastBuildSucceeded = true;
return {
message: (hasEmoji ? '✅ ' : '') + 'Build is successful',
contentImage: successImage,
status: 'success'
};
} else {
return {}
}
this.lastBuildSucceeded = false;
var message = '';
if (error.module && error.module.rawRequest)
message = error.module.rawRequest + '\n';
var hasEmoji = this.options.emoji;
if (error.error)
message = (hasEmoji ? '❌ ' : '') + 'Error: ' + message + error.error.toString();
else if (error.warning)
message = (hasEmoji ? '⚠️ ' : '') + 'Warning: ' + message + error.warning.toString();
else if (error.message) {
message = (hasEmoji ? '⚠️ ' : '') + 'Warning: ' + message + error.message.toString();
}
return {
message: stripANSI(message),
contentImage: contentImage,
status: status
}
};
WebpackNotifierPlugin.prototype.compilationDone = function (stats) {
var {message, contentImage, status} = this.compileEndOptions(stats);
if (message) {
var title = this.options.title ? this.options.title : 'Webpack'
if (typeof title === 'function') {
title = title({message: message, status: status})
}
var icon = (os.platform() === 'win32' || os.platform() === 'linux')
? contentImage
: undefined
var timeout = this.options.timeout ? this.options.timeout : 10
notifier.notify({
...this.options,
title,
message,
contentImage,
icon,
timeout
});
}
};
WebpackNotifierPlugin.prototype.apply = function (compiler) {
if (compiler.hooks) {
var plugin = {name: 'Notifier'};
compiler.hooks.done.tap(plugin, this.compilationDone.bind(this));
} else {
compiler.plugin('done', this.compilationDone.bind(this));
}
};