-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (56 loc) · 1.65 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
const spawn = require('child_process').spawn;
const _ = require('lodash');
const LineWrapper = require('stream-line-wrapper');
function ChildProcessPlugin (options) {
this.processes = [];
const defaultValues = {
command: '',
once: false,
runCount: 0,
prefix: '',
cwd: '.',
env: {
'PATH': process.env.PATH,
},
};
if (_.isString(options)) {
this.processes.push({
command: options
});
}
else if (_.isObject(options)) {
this.processes.push(options);
}
else if (_.isArray(options)) {
this.processes = _.filter(options, (proc) => {
return _.isObject(proc);
});
}
this.processes = _.map(this.processes, (proc) => {
return _.defaultsDeep(proc, defaultValues);
});
}
ChildProcessPlugin.prototype.apply = function (compiler) {
compiler.plugin('done', () => {
_.each(this.processes, (proc) => {
if (proc.once && proc.runCount >= 1)
return;
proc.runCount++;
const child = spawn(proc.command, {
cwd: proc.cwd,
env: proc.env,
stdio: ['ignore', 'pipe', 'pipe'],
shell: true,
});
const wrapperOut = new LineWrapper({
prefix: proc.prefix,
});
const wrapperErr = new LineWrapper({
prefix: proc.prefix,
});
child.stdout.pipe(wrapperOut).pipe(process.stdout);
child.stderr.pipe(wrapperErr).pipe(process.stderr);
});
});
};
module.exports = ChildProcessPlugin;