Skip to content

Commit e54b5db

Browse files
rusluxRuslan Roskoshnyj
authored and
Ruslan Roskoshnyj
committed
Add sync option: resolve #48
1 parent a65ae75 commit e54b5db

File tree

6 files changed

+215
-236
lines changed

6 files changed

+215
-236
lines changed

.babelrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"presets": ["es2015-rollup"]
2+
"presets": [["env", {"modules": false}]],
3+
"plugins": ["transform-runtime"],
4+
"runtimeHelpers": true
35
}

lib/index.js

Lines changed: 124 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,16 @@
11
'use strict';
22

3-
var asyncGenerator = function () {
4-
function AwaitValue(value) {
5-
this.value = value;
6-
}
7-
8-
function AsyncGenerator(gen) {
9-
var front, back;
10-
11-
function send(key, arg) {
12-
return new Promise(function (resolve, reject) {
13-
var request = {
14-
key: key,
15-
arg: arg,
16-
resolve: resolve,
17-
reject: reject,
18-
next: null
19-
};
20-
21-
if (back) {
22-
back = back.next = request;
23-
} else {
24-
front = back = request;
25-
resume(key, arg);
26-
}
27-
});
28-
}
29-
30-
function resume(key, arg) {
31-
try {
32-
var result = gen[key](arg);
33-
var value = result.value;
34-
35-
if (value instanceof AwaitValue) {
36-
Promise.resolve(value.value).then(function (arg) {
37-
resume("next", arg);
38-
}, function (arg) {
39-
resume("throw", arg);
40-
});
41-
} else {
42-
settle(result.done ? "return" : "normal", result.value);
43-
}
44-
} catch (err) {
45-
settle("throw", err);
46-
}
47-
}
48-
49-
function settle(type, value) {
50-
switch (type) {
51-
case "return":
52-
front.resolve({
53-
value: value,
54-
done: true
55-
});
56-
break;
57-
58-
case "throw":
59-
front.reject(value);
60-
break;
61-
62-
default:
63-
front.resolve({
64-
value: value,
65-
done: false
66-
});
67-
break;
68-
}
69-
70-
front = front.next;
71-
72-
if (front) {
73-
resume(front.key, front.arg);
74-
} else {
75-
back = null;
76-
}
77-
}
78-
79-
this._invoke = send;
80-
81-
if (typeof gen.return !== "function") {
82-
this.return = undefined;
83-
}
84-
}
85-
86-
if (typeof Symbol === "function" && Symbol.asyncIterator) {
87-
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
88-
return this;
89-
};
90-
}
91-
92-
AsyncGenerator.prototype.next = function (arg) {
93-
return this._invoke("next", arg);
94-
};
95-
96-
AsyncGenerator.prototype.throw = function (arg) {
97-
return this._invoke("throw", arg);
98-
};
99-
100-
AsyncGenerator.prototype.return = function (arg) {
101-
return this._invoke("return", arg);
102-
};
103-
104-
return {
105-
wrap: function (fn) {
106-
return function () {
107-
return new AsyncGenerator(fn.apply(this, arguments));
108-
};
109-
},
110-
await: function (value) {
111-
return new AwaitValue(value);
112-
}
113-
};
114-
}();
115-
116-
var classCallCheck = function (instance, Constructor) {
117-
if (!(instance instanceof Constructor)) {
118-
throw new TypeError("Cannot call a class as a function");
119-
}
120-
};
121-
122-
var createClass = function () {
123-
function defineProperties(target, props) {
124-
for (var i = 0; i < props.length; i++) {
125-
var descriptor = props[i];
126-
descriptor.enumerable = descriptor.enumerable || false;
127-
descriptor.configurable = true;
128-
if ("value" in descriptor) descriptor.writable = true;
129-
Object.defineProperty(target, descriptor.key, descriptor);
130-
}
131-
}
132-
133-
return function (Constructor, protoProps, staticProps) {
134-
if (protoProps) defineProperties(Constructor.prototype, protoProps);
135-
if (staticProps) defineProperties(Constructor, staticProps);
136-
return Constructor;
137-
};
138-
}();
3+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
1394

140-
var toArray = function (arr) {
141-
return Array.isArray(arr) ? arr : Array.from(arr);
142-
};
5+
var _getIterator = _interopDefault(require('babel-runtime/core-js/get-iterator'));
6+
var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator'));
7+
var _Promise = _interopDefault(require('babel-runtime/core-js/promise'));
8+
var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator'));
9+
var _toArray = _interopDefault(require('babel-runtime/helpers/toArray'));
10+
var _classCallCheck = _interopDefault(require('babel-runtime/helpers/classCallCheck'));
11+
var _createClass = _interopDefault(require('babel-runtime/helpers/createClass'));
12+
var child_process = require('child_process');
14313

144-
var spawn = require('child_process').spawn;
145-
var exec = require('child_process').exec;
14614
var os = require('os');
14715

14816
var defaultOptions = {
@@ -151,17 +19,22 @@ var defaultOptions = {
15119
onBuildExit: [],
15220
dev: true,
15321
verbose: false,
154-
safe: false
22+
safe: false,
23+
sync: false
15524
};
15625

15726
var WebpackShellPlugin = function () {
15827
function WebpackShellPlugin(options) {
159-
classCallCheck(this, WebpackShellPlugin);
28+
_classCallCheck(this, WebpackShellPlugin);
16029

16130
this.options = this.validateInput(this.mergeOptions(options, defaultOptions));
31+
32+
this.onCompilation = this.onCompilation.bind(this);
33+
this.onAfterEmit = this.onAfterEmit.bind(this);
34+
this.onDone = this.onDone.bind(this);
16235
}
16336

164-
createClass(WebpackShellPlugin, [{
37+
_createClass(WebpackShellPlugin, [{
16538
key: 'puts',
16639
value: function puts(error, stdout, stderr) {
16740
if (error) {
@@ -179,7 +52,7 @@ var WebpackShellPlugin = function () {
17952
value: function serializeScript(script) {
18053
if (typeof script === 'string') {
18154
var _script$split = script.split(' '),
182-
_script$split2 = toArray(_script$split),
55+
_script$split2 = _toArray(_script$split),
18356
_command = _script$split2[0],
18457
_args = _script$split2.slice(1);
18558

@@ -190,18 +63,83 @@ var WebpackShellPlugin = function () {
19063

19164
return { command: command, args: args };
19265
}
66+
}, {
67+
key: 'sleep',
68+
value: function () {
69+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(ms) {
70+
return _regeneratorRuntime.wrap(function _callee$(_context) {
71+
while (1) {
72+
switch (_context.prev = _context.next) {
73+
case 0:
74+
return _context.abrupt('return', new _Promise(function (resolve) {
75+
return setTimeout(resolve, ms);
76+
}));
77+
78+
case 1:
79+
case 'end':
80+
return _context.stop();
81+
}
82+
}
83+
}, _callee, this);
84+
}));
85+
86+
function sleep(_x) {
87+
return _ref.apply(this, arguments);
88+
}
89+
90+
return sleep;
91+
}()
19392
}, {
19493
key: 'handleScript',
19594
value: function handleScript(script) {
95+
var proc = null;
96+
19697
if (os.platform() === 'win32' || this.options.safe) {
197-
this.spreadStdoutAndStdErr(exec(script, this.puts));
98+
if (this.options.sync) {
99+
proc = child_process.execSync(script, { stdio: [0, 1, 2] });
100+
} else {
101+
proc = child_process.exec(script, this.puts);
102+
this.spreadStdoutAndStdErr(proc);
103+
}
198104
} else {
199105
var _serializeScript = this.serializeScript(script),
200106
command = _serializeScript.command,
201107
args = _serializeScript.args;
202108

203-
var proc = spawn(command, args, { stdio: 'inherit' });
204-
proc.on('close', this.puts);
109+
if (this.options.sync) {
110+
proc = child_process.spawnSync(command, args, { stdio: 'inherit' });
111+
} else {
112+
proc = child_process.spawn(command, args, { stdio: 'inherit' });
113+
proc.on('close', this.puts);
114+
}
115+
}
116+
}
117+
}, {
118+
key: 'handleScriptsOn',
119+
value: function handleScriptsOn(data) {
120+
var _iteratorNormalCompletion = true;
121+
var _didIteratorError = false;
122+
var _iteratorError = undefined;
123+
124+
try {
125+
for (var _iterator = _getIterator(data), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
126+
var item = _step.value;
127+
128+
this.handleScript(item);
129+
}
130+
} catch (err) {
131+
_didIteratorError = true;
132+
_iteratorError = err;
133+
} finally {
134+
try {
135+
if (!_iteratorNormalCompletion && _iterator.return) {
136+
_iterator.return();
137+
}
138+
} finally {
139+
if (_didIteratorError) {
140+
throw _iteratorError;
141+
}
142+
}
205143
}
206144
}
207145
}, {
@@ -231,48 +169,48 @@ var WebpackShellPlugin = function () {
231169
}, {
232170
key: 'apply',
233171
value: function apply(compiler) {
234-
var _this = this;
235-
236-
compiler.plugin('compilation', function (compilation) {
237-
if (_this.options.verbose) {
238-
console.log('Report compilation: ' + compilation);
239-
console.warn('WebpackShellPlugin [' + new Date() + ']: Verbose is being deprecated, please remove.');
240-
}
241-
if (_this.options.onBuildStart.length) {
242-
console.log('Executing pre-build scripts');
243-
for (var i = 0; i < _this.options.onBuildStart.length; i++) {
244-
_this.handleScript(_this.options.onBuildStart[i]);
245-
}
246-
if (_this.options.dev) {
247-
_this.options.onBuildStart = [];
248-
}
249-
}
250-
});
251-
252-
compiler.plugin('after-emit', function (compilation, callback) {
253-
if (_this.options.onBuildEnd.length) {
254-
console.log('Executing post-build scripts');
255-
for (var i = 0; i < _this.options.onBuildEnd.length; i++) {
256-
_this.handleScript(_this.options.onBuildEnd[i]);
257-
}
258-
if (_this.options.dev) {
259-
_this.options.onBuildEnd = [];
260-
}
172+
compiler.plugin('compilation', this.onCompilation);
173+
compiler.plugin('after-emit', this.onAfterEmit);
174+
compiler.plugin('done', this.onDone);
175+
}
176+
}, {
177+
key: 'onCompilation',
178+
value: function onCompilation(compilation) {
179+
if (this.options.verbose) {
180+
console.log('Report compilation: ' + compilation);
181+
console.warn('WebpackShellPlugin [' + new Date() + ']: Verbose is being deprecated, please remove.');
182+
}
183+
if (this.options.onBuildStart.length) {
184+
console.log('Executing pre-build scripts');
185+
this.handleScriptsOn(this.options.onBuildStart);
186+
if (this.options.dev) {
187+
this.options.onBuildStart = [];
261188
}
262-
callback();
263-
});
264-
265-
compiler.plugin('done', function () {
266-
if (_this.options.onBuildExit.length) {
267-
console.log('Executing additional scripts before exit');
268-
for (var i = 0; i < _this.options.onBuildExit.length; i++) {
269-
_this.handleScript(_this.options.onBuildExit[i]);
270-
}
189+
}
190+
}
191+
}, {
192+
key: 'onAfterEmit',
193+
value: function onAfterEmit(compilation, callback) {
194+
if (this.options.onBuildEnd.length) {
195+
console.log('Executing post-build scripts');
196+
this.handleScriptsOn(this.options.onBuildEnd);
197+
if (this.options.dev) {
198+
this.options.onBuildEnd = [];
271199
}
272-
});
200+
}
201+
callback();
202+
}
203+
}, {
204+
key: 'onDone',
205+
value: function onDone() {
206+
if (this.options.onBuildExit.length) {
207+
console.log('Executing additional scripts before exit');
208+
this.handleScriptsOn(this.options.onBuildExit);
209+
}
273210
}
274211
}]);
212+
275213
return WebpackShellPlugin;
276214
}();
277215

278-
module.exports = WebpackShellPlugin;
216+
module.exports = WebpackShellPlugin;

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@
3434
},
3535
"homepage": "https://github.com/1337programming/webpack-shell-plugin",
3636
"devDependencies": {
37-
"babel-core": "^6.7.6",
38-
"babel-preset-es2015-rollup": "^1.1.1",
37+
"babel-core": "^6.26.0",
38+
"babel-plugin-external-helpers": "^6.22.0",
39+
"babel-plugin-transform-runtime": "^6.23.0",
40+
"babel-polyfill": "^6.26.0",
41+
"babel-preset-env": "^1.6.1",
42+
"babel-runtime": "^6.26.0",
43+
"babelrc-rollup": "^3.0.0",
3944
"css-loader": "^0.23.1",
4045
"eslint": "^2.7.0",
41-
"rollup": "^0.25.8",
42-
"rollup-plugin-babel": "^2.4.0",
46+
"polyfill": "^0.1.0",
47+
"rollup": "^0.55.3",
48+
"rollup-plugin-babel": "^3.0.3",
4349
"style-loader": "^0.13.1",
4450
"webpack": "^1.13.1"
4551
}

0 commit comments

Comments
 (0)