-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (97 loc) · 2.31 KB
/
Copy pathindex.js
File metadata and controls
114 lines (97 loc) · 2.31 KB
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
/*
* Start anybar with:
* ANYBAR_TITLE=main ANYBAR_PORT=1738 open -na AnyBar
*/
const { validate } = require('schema-utils');
const dgram = require('dgram');
const anybarSocket = dgram.createSocket('udp4').unref();
let isEnabled = true;
anybarSocket.on('error', () => {
isEnabled = false;
});
const schema = {
type: 'object',
properties: {
port: {
type: 'number',
},
},
};
class AnybarWebpackPlugin {
constructor(options = {}) {
// TODO: make static.
const defaultOptions = {
port: 1738,
};
validate(schema, options, {
name: 'AnybarWebpackPlugin',
baseDataPath: 'options',
});
this.options = { ...defaultOptions, ...options };
this.send = ['hollow', 'green', 'yellow', 'exclamation'].reduce(
(map, color) => {
map[color] = this._send.bind(this, color);
return map;
},
{}
);
this.eventMapAsync = {
beforeRun: this.send['yellow'],
watchRun: this.send['yellow'],
beforeCompile: this.send['yellow'],
};
this.eventMapSync = {
failed: this.send['exclamation'],
watchClose: this.send['hollow'],
};
['SIGINT', 'SIGTERM'].forEach(ev => {
process.on(ev, () => {
this.stop(() => {
process.exit(0);
});
});
});
process.once('beforeExit', () => {
anybarSocket.ref();
this.stop(() => {
anybarSocket.unref();
});
});
}
_send(message, cb = () => {}) {
if (isEnabled)
anybarSocket.send(
message,
0,
message.length,
this.options.port,
null,
cb
);
else cb();
}
stop(cb = () => {}) {
this._send('hollow', cb);
}
apply(compiler) {
const pluginName = 'AnybarWebpackPlugin';
Object.keys(this.eventMapAsync).forEach(event => {
compiler.hooks[event].tapAsync(pluginName, (_, cb) => {
this.eventMapAsync[event](cb);
});
});
Object.keys(this.eventMapSync).forEach(event => {
compiler.hooks[event].tap(pluginName, () => {
this.eventMapSync[event]();
});
});
compiler.hooks.done.tapAsync(pluginName, (stats, cb) => {
if (stats.hasErrors()) {
this._send('exclamation', cb);
} else {
this._send('green', cb);
}
});
}
}
module.exports = AnybarWebpackPlugin;