Skip to content

Commit 7aaa389

Browse files
committed
Merge branch 'slightlytyler-master'
2 parents 6da5ce9 + 12f6a95 commit 7aaa389

File tree

3 files changed

+46
-64
lines changed

3 files changed

+46
-64
lines changed

lib/index.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,17 @@ 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: [],
144148
onBuildExit: [],
145149
dev: true,
146-
verbose: false,
147-
throwOnExecError: false
150+
verbose: false
148151
};
149152

150153
function puts(error, stdout, stderr) {
@@ -153,15 +156,29 @@ function puts(error, stdout, stderr) {
153156
}
154157
}
155158

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

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

167184
function validateInput(options) {
@@ -204,13 +221,7 @@ var WebpackShellPlugin = function () {
204221
}
205222
if (_this.options.onBuildStart.length) {
206223
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-
});
224+
_this.options.onBuildStart.forEach(handleScript);
214225
if (_this.options.dev) {
215226
_this.options.onBuildStart = [];
216227
}
@@ -220,13 +231,7 @@ var WebpackShellPlugin = function () {
220231
compiler.plugin('emit', function (compilation, callback) {
221232
if (_this.options.onBuildEnd.length) {
222233
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-
});
234+
_this.options.onBuildEnd.forEach(handleScript);
230235
if (_this.options.dev) {
231236
_this.options.onBuildEnd = [];
232237
}
@@ -237,13 +242,7 @@ var WebpackShellPlugin = function () {
237242
compiler.plugin('done', function () {
238243
if (_this.options.onBuildExit.length) {
239244
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-
});
245+
_this.options.onBuildExit.forEach(handleScript);
247246
}
248247
});
249248
}

src/webpack-shell-plugin.js

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
const exec = require('child_process').exec;
1+
const spawn = require('child_process').spawn;
22
const defaultOptions = {
33
onBuildStart: [],
44
onBuildEnd: [],
55
onBuildExit: [],
66
dev: true,
7-
verbose: false,
8-
throwOnExecError: false
7+
verbose: false
98
};
109

1110
function puts(error, stdout, stderr) {
@@ -14,15 +13,20 @@ function puts(error, stdout, stderr) {
1413
}
1514
}
1615

17-
function putsThrow(error, stdout, stderr) {
18-
if (error) {
19-
throw error;
16+
function serializeScript(script) {
17+
if (typeof script === 'string') {
18+
const [command, ...args] = script.split(' ');
19+
return {command, args};
2020
}
21+
const {command, args} = script;
22+
return {command, args};
2123
}
2224

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

2832
function validateInput(options) {
@@ -60,14 +64,7 @@ export default class WebpackShellPlugin {
6064
}
6165
if (this.options.onBuildStart.length) {
6266
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-
});
67+
this.options.onBuildStart.forEach(handleScript);
7168
if (this.options.dev) {
7269
this.options.onBuildStart = [];
7370
}
@@ -77,14 +74,7 @@ export default class WebpackShellPlugin {
7774
compiler.plugin('emit', (compilation, callback) => {
7875
if (this.options.onBuildEnd.length) {
7976
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-
});
77+
this.options.onBuildEnd.forEach(handleScript);
8878
if (this.options.dev) {
8979
this.options.onBuildEnd = [];
9080
}
@@ -95,14 +85,7 @@ export default class WebpackShellPlugin {
9585
compiler.plugin('done', () => {
9686
if (this.options.onBuildExit.length) {
9787
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-
});
88+
this.options.onBuildExit.forEach(handleScript);
10689
}
10790
});
10891
}

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ module.exports = {
2222
new WebpackShellPlugin({onBuildStart:['echo "Webpack Start"'], onBuildEnd:['echo "Webpack End"']}),
2323
new webpack.HotModuleReplacementPlugin()
2424
]
25-
};
25+
};

0 commit comments

Comments
 (0)