Skip to content

Commit 86b4616

Browse files
authored
Merge pull request #18 from gvandegrift/throw-on-exec-error
Fix for issue #16.
2 parents f57d7d8 + cd02753 commit 86b4616

File tree

2 files changed

+88
-79
lines changed

2 files changed

+88
-79
lines changed

lib/index.js

Lines changed: 58 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,13 @@
11
'use strict';
22

3-
var babelHelpers = {};
4-
5-
babelHelpers.classCallCheck = function (instance, Constructor) {
6-
if (!(instance instanceof Constructor)) {
7-
throw new TypeError("Cannot call a class as a function");
8-
}
9-
};
10-
11-
babelHelpers.createClass = function () {
12-
function defineProperties(target, props) {
13-
for (var i = 0; i < props.length; i++) {
14-
var descriptor = props[i];
15-
descriptor.enumerable = descriptor.enumerable || false;
16-
descriptor.configurable = true;
17-
if ("value" in descriptor) descriptor.writable = true;
18-
Object.defineProperty(target, descriptor.key, descriptor);
19-
}
20-
}
21-
22-
return function (Constructor, protoProps, staticProps) {
23-
if (protoProps) defineProperties(Constructor.prototype, protoProps);
24-
if (staticProps) defineProperties(Constructor, staticProps);
25-
return Constructor;
26-
};
27-
}();
28-
29-
babelHelpers;
30-
31-
var exec = require('child_process').exec;
32-
var defaultOptions = {
3+
const exec = require('child_process').exec;
4+
const defaultOptions = {
335
onBuildStart: [],
346
onBuildEnd: [],
357
onBuildExit: [],
368
dev: true,
37-
verbose: false
9+
verbose: false,
10+
throwOnExecError: false
3811
};
3912

4013
function puts(error, stdout, stderr) {
@@ -43,6 +16,12 @@ function puts(error, stdout, stderr) {
4316
}
4417
}
4518

19+
function putsThrow(error, stdout, stderr) {
20+
if (error) {
21+
throw error;
22+
}
23+
}
24+
4625
function spreadStdoutAndStdErr(proc) {
4726
proc.stdout.pipe(process.stdout);
4827
proc.stderr.pipe(process.stdout);
@@ -62,65 +41,70 @@ function validateInput(options) {
6241
}
6342

6443
function mergeOptions(options, defaults) {
65-
for (var key in defaults) {
44+
for (const key in defaults) {
6645
if (options.hasOwnProperty(key)) {
6746
defaults[key] = options[key];
6847
}
6948
}
7049
return defaults;
7150
}
7251

73-
var WebpackShellPlugin = function () {
74-
function WebpackShellPlugin(options) {
75-
babelHelpers.classCallCheck(this, WebpackShellPlugin);
76-
52+
class WebpackShellPlugin {
53+
constructor(options) {
7754
this.options = validateInput(mergeOptions(options, defaultOptions));
7855
}
7956

80-
babelHelpers.createClass(WebpackShellPlugin, [{
81-
key: 'apply',
82-
value: function apply(compiler) {
83-
var _this = this;
84-
85-
compiler.plugin('compilation', function (compilation) {
86-
if (_this.options.verbose) {
87-
console.log('Report compilation: ' + compilation);
88-
}
89-
if (_this.options.onBuildStart.length) {
90-
console.log('Executing pre-build scripts');
91-
_this.options.onBuildStart.forEach(function (script) {
57+
apply(compiler) {
58+
59+
compiler.plugin('compilation', compilation => {
60+
if (this.options.verbose) {
61+
console.log(`Report compilation: ${ compilation }`);
62+
}
63+
if (this.options.onBuildStart.length) {
64+
console.log('Executing pre-build scripts');
65+
this.options.onBuildStart.forEach(script => {
66+
if (this.options.throwOnExecError) {
67+
spreadStdoutAndStdErr(exec(script, putsThrow));
68+
} else {
9269
spreadStdoutAndStdErr(exec(script, puts));
93-
});
94-
if (_this.options.dev) {
95-
_this.options.onBuildStart = [];
9670
}
71+
});
72+
if (this.options.dev) {
73+
this.options.onBuildStart = [];
9774
}
98-
});
99-
100-
compiler.plugin('emit', function (compilation, callback) {
101-
if (_this.options.onBuildEnd.length) {
102-
console.log('Executing post-build scripts');
103-
_this.options.onBuildEnd.forEach(function (script) {
75+
}
76+
});
77+
78+
compiler.plugin('emit', (compilation, callback) => {
79+
if (this.options.onBuildEnd.length) {
80+
console.log('Executing post-build scripts');
81+
this.options.onBuildEnd.forEach(script => {
82+
if (this.options.throwOnExecError) {
83+
spreadStdoutAndStdErr(exec(script, putsThrow));
84+
} else {
10485
spreadStdoutAndStdErr(exec(script, puts));
105-
});
106-
if (_this.options.dev) {
107-
_this.options.onBuildEnd = [];
10886
}
87+
});
88+
if (this.options.dev) {
89+
this.options.onBuildEnd = [];
10990
}
110-
callback();
111-
});
112-
113-
compiler.plugin('done', function () {
114-
if (_this.options.onBuildExit.length) {
115-
console.log('Executing additional scripts before exit');
116-
_this.options.onBuildExit.forEach(function (script) {
91+
}
92+
callback();
93+
});
94+
95+
compiler.plugin('done', () => {
96+
if (this.options.onBuildExit.length) {
97+
console.log('Executing additional scripts before exit');
98+
this.options.onBuildExit.forEach(script => {
99+
if (this.options.throwOnExecError) {
100+
spreadStdoutAndStdErr(exec(script, putsThrow));
101+
} else {
117102
spreadStdoutAndStdErr(exec(script, puts));
118-
});
119-
}
120-
});
121-
}
122-
}]);
123-
return WebpackShellPlugin;
124-
}();
103+
}
104+
});
105+
}
106+
});
107+
}
108+
}
125109

126110
module.exports = WebpackShellPlugin;

src/webpack-shell-plugin.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const defaultOptions = {
44
onBuildEnd: [],
55
onBuildExit: [],
66
dev: true,
7-
verbose: false
7+
verbose: false,
8+
throwOnExecError: false
89
};
910

1011
function puts(error, stdout, stderr) {
@@ -13,6 +14,12 @@ function puts(error, stdout, stderr) {
1314
}
1415
}
1516

17+
function putsThrow(error, stdout, stderr) {
18+
if (error) {
19+
throw error;
20+
}
21+
}
22+
1623
function spreadStdoutAndStdErr(proc) {
1724
proc.stdout.pipe(process.stdout);
1825
proc.stderr.pipe(process.stdout);
@@ -32,7 +39,7 @@ function validateInput(options) {
3239
}
3340

3441
function mergeOptions(options, defaults) {
35-
for (let key in defaults) {
42+
for (const key in defaults) {
3643
if (options.hasOwnProperty(key)) {
3744
defaults[key] = options[key];
3845
}
@@ -54,7 +61,13 @@ export default class WebpackShellPlugin {
5461
if (this.options.onBuildStart.length) {
5562
console.log('Executing pre-build scripts');
5663
this.options.onBuildStart.forEach((script) => {
57-
spreadStdoutAndStdErr(exec(script, puts));
64+
if (this.options.throwOnExecError) {
65+
spreadStdoutAndStdErr(exec(script, putsThrow));
66+
}
67+
else
68+
{
69+
spreadStdoutAndStdErr(exec(script, puts));
70+
}
5871
});
5972
if (this.options.dev) {
6073
this.options.onBuildStart = [];
@@ -66,7 +79,13 @@ export default class WebpackShellPlugin {
6679
if (this.options.onBuildEnd.length) {
6780
console.log('Executing post-build scripts');
6881
this.options.onBuildEnd.forEach((script) => {
69-
spreadStdoutAndStdErr(exec(script, puts));
82+
if (this.options.throwOnExecError) {
83+
spreadStdoutAndStdErr(exec(script, putsThrow));
84+
}
85+
else
86+
{
87+
spreadStdoutAndStdErr(exec(script, puts));
88+
}
7089
});
7190
if (this.options.dev) {
7291
this.options.onBuildEnd = [];
@@ -79,7 +98,13 @@ export default class WebpackShellPlugin {
7998
if (this.options.onBuildExit.length) {
8099
console.log('Executing additional scripts before exit');
81100
this.options.onBuildExit.forEach((script) => {
82-
spreadStdoutAndStdErr(exec(script, puts));
101+
if (this.options.throwOnExecError) {
102+
spreadStdoutAndStdErr(exec(script, putsThrow));
103+
}
104+
else
105+
{
106+
spreadStdoutAndStdErr(exec(script, puts));
107+
}
83108
});
84109
}
85110
});

0 commit comments

Comments
 (0)