Skip to content

Commit cf18d39

Browse files
committed
Reintroduce exec for windows.
1 parent b91968f commit cf18d39

File tree

3 files changed

+138
-96
lines changed

3 files changed

+138
-96
lines changed

lib/index.js

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -142,75 +142,92 @@ var toArray = function (arr) {
142142
};
143143

144144
var spawn = require('child_process').spawn;
145+
var exec = require('child_process').exec;
146+
var os = require('os');
147+
145148
var defaultOptions = {
146149
onBuildStart: [],
147150
onBuildEnd: [],
148151
onBuildExit: [],
149152
dev: true,
150-
throwOnExecError: false,
151153
verbose: false
152154
};
153155

154-
function puts(error, stdout, stderr) {
155-
if (error) {
156-
throw error;
157-
}
158-
}
159-
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 };
168-
}
169-
var command = script.command,
170-
args = script.args;
171-
172-
return { command: command, args: args };
173-
}
174-
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-
proc.on('close', puts);
182-
}
183-
184-
function validateInput(options) {
185-
if (typeof options.onBuildStart === 'string') {
186-
options.onBuildStart = options.onBuildStart.split('&&');
187-
}
188-
if (typeof options.onBuildEnd === 'string') {
189-
options.onBuildEnd = options.onBuildEnd.split('&&');
190-
}
191-
if (typeof options.onBuildExit === 'string') {
192-
options.onBuildExit = options.onBuildExit.split('&&');
193-
}
194-
return options;
195-
}
196-
197-
function mergeOptions(options, defaults) {
198-
for (var key in defaults) {
199-
if (options.hasOwnProperty(key)) {
200-
defaults[key] = options[key];
201-
}
202-
}
203-
return defaults;
204-
}
205-
206156
var WebpackShellPlugin = function () {
207157
function WebpackShellPlugin(options) {
208158
classCallCheck(this, WebpackShellPlugin);
209159

210-
this.options = validateInput(mergeOptions(options, defaultOptions));
160+
this.options = this.validateInput(this.mergeOptions(options, defaultOptions));
211161
}
212162

213163
createClass(WebpackShellPlugin, [{
164+
key: 'puts',
165+
value: function puts(error, stdout, stderr) {
166+
if (error) {
167+
throw error;
168+
}
169+
}
170+
}, {
171+
key: 'spreadStdoutAndStdErr',
172+
value: function spreadStdoutAndStdErr(proc) {
173+
proc.stdout.pipe(process.stdout);
174+
proc.stderr.pipe(process.stdout);
175+
}
176+
}, {
177+
key: 'serializeScript',
178+
value: function serializeScript(script) {
179+
if (typeof script === 'string') {
180+
var _script$split = script.split(' '),
181+
_script$split2 = toArray(_script$split),
182+
_command = _script$split2[0],
183+
_args = _script$split2.slice(1);
184+
185+
return { command: _command, args: _args };
186+
}
187+
var command = script.command,
188+
args = script.args;
189+
190+
return { command: command, args: args };
191+
}
192+
}, {
193+
key: 'handleScript',
194+
value: function handleScript(script) {
195+
if (os.platform() === 'win32') {
196+
this.spreadStdoutAndStdErr(exec(script, puts));
197+
} else {
198+
var _serializeScript = this.serializeScript(script),
199+
command = _serializeScript.command,
200+
args = _serializeScript.args;
201+
202+
var proc = spawn(command, args, { stdio: 'inherit' });
203+
proc.on('close', this.puts);
204+
}
205+
}
206+
}, {
207+
key: 'validateInput',
208+
value: function validateInput(options) {
209+
if (typeof options.onBuildStart === 'string') {
210+
options.onBuildStart = options.onBuildStart.split('&&');
211+
}
212+
if (typeof options.onBuildEnd === 'string') {
213+
options.onBuildEnd = options.onBuildEnd.split('&&');
214+
}
215+
if (typeof options.onBuildExit === 'string') {
216+
options.onBuildExit = options.onBuildExit.split('&&');
217+
}
218+
return options;
219+
}
220+
}, {
221+
key: 'mergeOptions',
222+
value: function mergeOptions(options, defaults) {
223+
for (var key in defaults) {
224+
if (options.hasOwnProperty(key)) {
225+
defaults[key] = options[key];
226+
}
227+
}
228+
return defaults;
229+
}
230+
}, {
214231
key: 'apply',
215232
value: function apply(compiler) {
216233
var _this = this;
@@ -221,7 +238,9 @@ var WebpackShellPlugin = function () {
221238
}
222239
if (_this.options.onBuildStart.length) {
223240
console.log('Executing pre-build scripts');
224-
_this.options.onBuildStart.forEach(handleScript);
241+
for (var i = 0; i < _this.options.onBuildStart.length; i++) {
242+
_this.handleScript(_this.options.onBuildStart[i]);
243+
}
225244
if (_this.options.dev) {
226245
_this.options.onBuildStart = [];
227246
}
@@ -231,7 +250,9 @@ var WebpackShellPlugin = function () {
231250
compiler.plugin('emit', function (compilation, callback) {
232251
if (_this.options.onBuildEnd.length) {
233252
console.log('Executing post-build scripts');
234-
_this.options.onBuildEnd.forEach(handleScript);
253+
for (var i = 0; i < _this.options.onBuildEnd.length; i++) {
254+
_this.handleScript(_this.options.onBuildEnd[i]);
255+
}
235256
if (_this.options.dev) {
236257
_this.options.onBuildEnd = [];
237258
}
@@ -242,7 +263,9 @@ var WebpackShellPlugin = function () {
242263
compiler.plugin('done', function () {
243264
if (_this.options.onBuildExit.length) {
244265
console.log('Executing additional scripts before exit');
245-
_this.options.onBuildExit.forEach(handleScript);
266+
for (var i = 0; i < _this.options.onBuildExit.length; i++) {
267+
_this.handleScript(_this.options.onBuildExit[i]);
268+
}
246269
}
247270
});
248271
}

src/webpack-shell-plugin.js

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const spawn = require('child_process').spawn;
2+
const exec = require('child_process').exec;
3+
const os = require('os');
4+
25
const defaultOptions = {
36
onBuildStart: [],
47
onBuildEnd: [],
@@ -7,52 +10,62 @@ const defaultOptions = {
710
verbose: false
811
};
912

10-
function puts(error, stdout, stderr) {
11-
if (error) {
12-
throw error;
13-
}
14-
}
1513

16-
function serializeScript(script) {
17-
if (typeof script === 'string') {
18-
const [command, ...args] = script.split(' ');
19-
return {command, args};
14+
export default class WebpackShellPlugin {
15+
constructor(options) {
16+
this.options = this.validateInput(this.mergeOptions(options, defaultOptions));
2017
}
21-
const {command, args} = script;
22-
return {command, args};
23-
}
2418

25-
function handleScript(script) {
26-
const {command, args} = serializeScript(script);
27-
const proc = spawn(command, args, {stdio: 'inherit'});
28-
proc.on('close', puts);
29-
}
19+
puts(error, stdout, stderr) {
20+
if (error) {
21+
throw error;
22+
}
23+
}
3024

31-
function validateInput(options) {
32-
if (typeof options.onBuildStart === 'string') {
33-
options.onBuildStart = options.onBuildStart.split('&&');
25+
spreadStdoutAndStdErr(proc) {
26+
proc.stdout.pipe(process.stdout);
27+
proc.stderr.pipe(process.stdout);
3428
}
35-
if (typeof options.onBuildEnd === 'string') {
36-
options.onBuildEnd = options.onBuildEnd.split('&&');
29+
30+
serializeScript(script) {
31+
if (typeof script === 'string') {
32+
const [command, ...args] = script.split(' ');
33+
return {command, args};
34+
}
35+
const {command, args} = script;
36+
return {command, args};
3737
}
38-
if (typeof options.onBuildExit === 'string') {
39-
options.onBuildExit = options.onBuildExit.split('&&');
38+
39+
handleScript(script) {
40+
if (os.platform() === 'win32') {
41+
this.spreadStdoutAndStdErr(exec(script, puts));
42+
} else {
43+
const {command, args} = this.serializeScript(script);
44+
const proc = spawn(command, args, {stdio: 'inherit'});
45+
proc.on('close', this.puts);
46+
}
4047
}
41-
return options;
42-
}
4348

44-
function mergeOptions(options, defaults) {
45-
for (const key in defaults) {
46-
if (options.hasOwnProperty(key)) {
47-
defaults[key] = options[key];
49+
validateInput(options) {
50+
if (typeof options.onBuildStart === 'string') {
51+
options.onBuildStart = options.onBuildStart.split('&&');
52+
}
53+
if (typeof options.onBuildEnd === 'string') {
54+
options.onBuildEnd = options.onBuildEnd.split('&&');
4855
}
56+
if (typeof options.onBuildExit === 'string') {
57+
options.onBuildExit = options.onBuildExit.split('&&');
58+
}
59+
return options;
4960
}
50-
return defaults;
51-
}
5261

53-
export default class WebpackShellPlugin {
54-
constructor(options) {
55-
this.options = validateInput(mergeOptions(options, defaultOptions));
62+
mergeOptions(options, defaults) {
63+
for (const key in defaults) {
64+
if (options.hasOwnProperty(key)) {
65+
defaults[key] = options[key];
66+
}
67+
}
68+
return defaults;
5669
}
5770

5871
apply(compiler) {
@@ -63,7 +76,9 @@ export default class WebpackShellPlugin {
6376
}
6477
if (this.options.onBuildStart.length) {
6578
console.log('Executing pre-build scripts');
66-
this.options.onBuildStart.forEach(handleScript);
79+
for (let i = 0; i < this.options.onBuildStart.length; i++) {
80+
this.handleScript(this.options.onBuildStart[i]);
81+
}
6782
if (this.options.dev) {
6883
this.options.onBuildStart = [];
6984
}
@@ -73,7 +88,9 @@ export default class WebpackShellPlugin {
7388
compiler.plugin('emit', (compilation, callback) => {
7489
if (this.options.onBuildEnd.length) {
7590
console.log('Executing post-build scripts');
76-
this.options.onBuildEnd.forEach(handleScript);
91+
for (let i = 0; i < this.options.onBuildEnd.length; i++) {
92+
this.handleScript(this.options.onBuildEnd[i]);
93+
}
7794
if (this.options.dev) {
7895
this.options.onBuildEnd = [];
7996
}
@@ -84,7 +101,9 @@ export default class WebpackShellPlugin {
84101
compiler.plugin('done', () => {
85102
if (this.options.onBuildExit.length) {
86103
console.log('Executing additional scripts before exit');
87-
this.options.onBuildExit.forEach(handleScript);
104+
for (let i = 0; i < this.options.onBuildExit.length; i++) {
105+
this.handleScript(this.options.onBuildExit[i]);
106+
}
88107
}
89108
});
90109
}

webpack.config.js

Lines changed: 1 addition & 1 deletion
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:['node test.js']}),
22+
new WebpackShellPlugin({onBuildStart:['echo "Webpack Start"'], onBuildEnd:['echo "Webpack End"']}),
2323
new webpack.HotModuleReplacementPlugin()
2424
]
2525
};

0 commit comments

Comments
 (0)