Skip to content

Commit ef95e6a

Browse files
committed
Child Process inside of exec.
2 parents 6da5ce9 + 0d3a044 commit ef95e6a

File tree

3 files changed

+45
-61
lines changed

3 files changed

+45
-61
lines changed

lib/index.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ var createClass = function () {
137137
};
138138
}();
139139

140-
var exec = require('child_process').exec;
140+
var toArray = function (arr) {
141+
return Array.isArray(arr) ? arr : Array.from(arr);
142+
};
143+
144+
var spawn = require('child_process').spawn;
141145
var defaultOptions = {
142146
onBuildStart: [],
143147
onBuildEnd: [],
@@ -153,15 +157,29 @@ function puts(error, stdout, stderr) {
153157
}
154158
}
155159

156-
function putsThrow(error, stdout, stderr) {
157-
if (error) {
158-
throw error;
160+
function serializeScript(script) {
161+
if (typeof script === 'string') {
162+
var _script$split = script.split(' '),
163+
_script$split2 = toArray(_script$split),
164+
_command = _script$split2[0],
165+
_args = _script$split2.slice(1);
166+
167+
return { command: _command, args: _args };
159168
}
169+
var command = script.command,
170+
args = script.args;
171+
172+
return { command: command, args: args };
160173
}
161174

162-
function spreadStdoutAndStdErr(proc) {
163-
proc.stdout.pipe(process.stdout);
164-
proc.stderr.pipe(process.stdout);
175+
function handleScript(script) {
176+
var _serializeScript = serializeScript(script),
177+
command = _serializeScript.command,
178+
args = _serializeScript.args;
179+
180+
var proc = spawn(command, args, { stdio: 'inherit' });
181+
182+
proc.on('close', puts);
165183
}
166184

167185
function validateInput(options) {
@@ -204,13 +222,7 @@ var WebpackShellPlugin = function () {
204222
}
205223
if (_this.options.onBuildStart.length) {
206224
console.log('Executing pre-build scripts');
207-
_this.options.onBuildStart.forEach(function (script) {
208-
if (_this.options.throwOnExecError) {
209-
spreadStdoutAndStdErr(exec(script, putsThrow));
210-
} else {
211-
spreadStdoutAndStdErr(exec(script, puts));
212-
}
213-
});
225+
_this.options.onBuildStart.forEach(handleScript);
214226
if (_this.options.dev) {
215227
_this.options.onBuildStart = [];
216228
}
@@ -220,13 +232,7 @@ var WebpackShellPlugin = function () {
220232
compiler.plugin('emit', function (compilation, callback) {
221233
if (_this.options.onBuildEnd.length) {
222234
console.log('Executing post-build scripts');
223-
_this.options.onBuildEnd.forEach(function (script) {
224-
if (_this.options.throwOnExecError) {
225-
spreadStdoutAndStdErr(exec(script, putsThrow));
226-
} else {
227-
spreadStdoutAndStdErr(exec(script, puts));
228-
}
229-
});
235+
_this.options.onBuildEnd.forEach(handleScript);
230236
if (_this.options.dev) {
231237
_this.options.onBuildEnd = [];
232238
}
@@ -237,13 +243,7 @@ var WebpackShellPlugin = function () {
237243
compiler.plugin('done', function () {
238244
if (_this.options.onBuildExit.length) {
239245
console.log('Executing additional scripts before exit');
240-
_this.options.onBuildExit.forEach(function (script) {
241-
if (_this.options.throwOnExecError) {
242-
spreadStdoutAndStdErr(exec(script, putsThrow));
243-
} else {
244-
spreadStdoutAndStdErr(exec(script, puts));
245-
}
246-
});
246+
_this.options.onBuildExit.forEach(handleScript);
247247
}
248248
});
249249
}

src/webpack-shell-plugin.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const exec = require('child_process').exec;
1+
const spawn = require('child_process').spawn;
22
const defaultOptions = {
33
onBuildStart: [],
44
onBuildEnd: [],
@@ -14,15 +14,20 @@ function puts(error, stdout, stderr) {
1414
}
1515
}
1616

17-
function putsThrow(error, stdout, stderr) {
18-
if (error) {
19-
throw error;
17+
function serializeScript(script) {
18+
if (typeof script === 'string') {
19+
const [command, ...args] = script.split(' ');
20+
return {command, args};
2021
}
22+
const {command, args} = script;
23+
return {command, args};
2124
}
2225

23-
function spreadStdoutAndStdErr(proc) {
24-
proc.stdout.pipe(process.stdout);
25-
proc.stderr.pipe(process.stdout);
26+
function handleScript(script) {
27+
const {command, args} = serializeScript(script);
28+
const proc = spawn(command, args, {stdio: 'inherit'});
29+
30+
proc.on('close', puts);
2631
}
2732

2833
function validateInput(options) {
@@ -60,14 +65,7 @@ export default class WebpackShellPlugin {
6065
}
6166
if (this.options.onBuildStart.length) {
6267
console.log('Executing pre-build scripts');
63-
this.options.onBuildStart.forEach((script) => {
64-
if (this.options.throwOnExecError) {
65-
spreadStdoutAndStdErr(exec(script, putsThrow));
66-
}
67-
else {
68-
spreadStdoutAndStdErr(exec(script, puts));
69-
}
70-
});
68+
this.options.onBuildStart.forEach(handleScript);
7169
if (this.options.dev) {
7270
this.options.onBuildStart = [];
7371
}
@@ -77,14 +75,7 @@ export default class WebpackShellPlugin {
7775
compiler.plugin('emit', (compilation, callback) => {
7876
if (this.options.onBuildEnd.length) {
7977
console.log('Executing post-build scripts');
80-
this.options.onBuildEnd.forEach((script) => {
81-
if (this.options.throwOnExecError) {
82-
spreadStdoutAndStdErr(exec(script, putsThrow));
83-
}
84-
else {
85-
spreadStdoutAndStdErr(exec(script, puts));
86-
}
87-
});
78+
this.options.onBuildEnd.forEach(handleScript);
8879
if (this.options.dev) {
8980
this.options.onBuildEnd = [];
9081
}
@@ -95,14 +86,7 @@ export default class WebpackShellPlugin {
9586
compiler.plugin('done', () => {
9687
if (this.options.onBuildExit.length) {
9788
console.log('Executing additional scripts before exit');
98-
this.options.onBuildExit.forEach((script) => {
99-
if (this.options.throwOnExecError) {
100-
spreadStdoutAndStdErr(exec(script, putsThrow));
101-
}
102-
else {
103-
spreadStdoutAndStdErr(exec(script, puts));
104-
}
105-
});
89+
this.options.onBuildExit.forEach(handleScript);
10690
}
10791
});
10892
}

webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
]
2020
},
2121
plugins: [
22-
new WebpackShellPlugin({onBuildStart:['echo "Webpack Start"'], onBuildEnd:['echo "Webpack End"']}),
22+
new WebpackShellPlugin({onBuildStart:['node test.js'], onBuildEnd:['echo "Webpack End"']}),
2323
new webpack.HotModuleReplacementPlugin()
2424
]
25-
};
25+
};

0 commit comments

Comments
 (0)