Skip to content

Add sync option: resolve #48 #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"presets": ["es2015-rollup"]
"presets": [["env", {"modules": false}]],
"plugins": ["transform-runtime"],
"runtimeHelpers": true
}
310 changes: 124 additions & 186 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,148 +1,16 @@
'use strict';

var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}

function AsyncGenerator(gen) {
var front, back;

function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};

if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}

function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;

if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}

function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;

case "throw":
front.reject(value);
break;

default:
front.resolve({
value: value,
done: false
});
break;
}

front = front.next;

if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}

this._invoke = send;

if (typeof gen.return !== "function") {
this.return = undefined;
}
}

if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}

AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};

AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};

AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};

return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();

var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};

var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}

return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
};
var _getIterator = _interopDefault(require('babel-runtime/core-js/get-iterator'));
var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator'));
var _Promise = _interopDefault(require('babel-runtime/core-js/promise'));
var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator'));
var _toArray = _interopDefault(require('babel-runtime/helpers/toArray'));
var _classCallCheck = _interopDefault(require('babel-runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('babel-runtime/helpers/createClass'));
var child_process = require('child_process');

var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var os = require('os');

var defaultOptions = {
Expand All @@ -151,17 +19,22 @@ var defaultOptions = {
onBuildExit: [],
dev: true,
verbose: false,
safe: false
safe: false,
sync: false
};

var WebpackShellPlugin = function () {
function WebpackShellPlugin(options) {
classCallCheck(this, WebpackShellPlugin);
_classCallCheck(this, WebpackShellPlugin);

this.options = this.validateInput(this.mergeOptions(options, defaultOptions));

this.onCompilation = this.onCompilation.bind(this);
this.onAfterEmit = this.onAfterEmit.bind(this);
this.onDone = this.onDone.bind(this);
}

createClass(WebpackShellPlugin, [{
_createClass(WebpackShellPlugin, [{
key: 'puts',
value: function puts(error, stdout, stderr) {
if (error) {
Expand All @@ -179,7 +52,7 @@ var WebpackShellPlugin = function () {
value: function serializeScript(script) {
if (typeof script === 'string') {
var _script$split = script.split(' '),
_script$split2 = toArray(_script$split),
_script$split2 = _toArray(_script$split),
_command = _script$split2[0],
_args = _script$split2.slice(1);

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

return { command: command, args: args };
}
}, {
key: 'sleep',
value: function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(ms) {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt('return', new _Promise(function (resolve) {
return setTimeout(resolve, ms);
}));

case 1:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));

function sleep(_x) {
return _ref.apply(this, arguments);
}

return sleep;
}()
}, {
key: 'handleScript',
value: function handleScript(script) {
var proc = null;

if (os.platform() === 'win32' || this.options.safe) {
this.spreadStdoutAndStdErr(exec(script, this.puts));
if (this.options.sync) {
proc = child_process.execSync(script, { stdio: [0, 1, 2] });
} else {
proc = child_process.exec(script, this.puts);
this.spreadStdoutAndStdErr(proc);
}
} else {
var _serializeScript = this.serializeScript(script),
command = _serializeScript.command,
args = _serializeScript.args;

var proc = spawn(command, args, { stdio: 'inherit' });
proc.on('close', this.puts);
if (this.options.sync) {
proc = child_process.spawnSync(command, args, { stdio: 'inherit' });
} else {
proc = child_process.spawn(command, args, { stdio: 'inherit' });
proc.on('close', this.puts);
}
}
}
}, {
key: 'handleScriptsOn',
value: function handleScriptsOn(data) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = _getIterator(data), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var item = _step.value;

this.handleScript(item);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}, {
Expand Down Expand Up @@ -231,48 +169,48 @@ var WebpackShellPlugin = function () {
}, {
key: 'apply',
value: function apply(compiler) {
var _this = this;

compiler.plugin('compilation', function (compilation) {
if (_this.options.verbose) {
console.log('Report compilation: ' + compilation);
console.warn('WebpackShellPlugin [' + new Date() + ']: Verbose is being deprecated, please remove.');
}
if (_this.options.onBuildStart.length) {
console.log('Executing pre-build scripts');
for (var i = 0; i < _this.options.onBuildStart.length; i++) {
_this.handleScript(_this.options.onBuildStart[i]);
}
if (_this.options.dev) {
_this.options.onBuildStart = [];
}
}
});

compiler.plugin('after-emit', function (compilation, callback) {
if (_this.options.onBuildEnd.length) {
console.log('Executing post-build scripts');
for (var i = 0; i < _this.options.onBuildEnd.length; i++) {
_this.handleScript(_this.options.onBuildEnd[i]);
}
if (_this.options.dev) {
_this.options.onBuildEnd = [];
}
compiler.plugin('compilation', this.onCompilation);
compiler.plugin('after-emit', this.onAfterEmit);
compiler.plugin('done', this.onDone);
}
}, {
key: 'onCompilation',
value: function onCompilation(compilation) {
if (this.options.verbose) {
console.log('Report compilation: ' + compilation);
console.warn('WebpackShellPlugin [' + new Date() + ']: Verbose is being deprecated, please remove.');
}
if (this.options.onBuildStart.length) {
console.log('Executing pre-build scripts');
this.handleScriptsOn(this.options.onBuildStart);
if (this.options.dev) {
this.options.onBuildStart = [];
}
callback();
});

compiler.plugin('done', function () {
if (_this.options.onBuildExit.length) {
console.log('Executing additional scripts before exit');
for (var i = 0; i < _this.options.onBuildExit.length; i++) {
_this.handleScript(_this.options.onBuildExit[i]);
}
}
}
}, {
key: 'onAfterEmit',
value: function onAfterEmit(compilation, callback) {
if (this.options.onBuildEnd.length) {
console.log('Executing post-build scripts');
this.handleScriptsOn(this.options.onBuildEnd);
if (this.options.dev) {
this.options.onBuildEnd = [];
}
});
}
callback();
}
}, {
key: 'onDone',
value: function onDone() {
if (this.options.onBuildExit.length) {
console.log('Executing additional scripts before exit');
this.handleScriptsOn(this.options.onBuildExit);
}
}
}]);

return WebpackShellPlugin;
}();

module.exports = WebpackShellPlugin;
module.exports = WebpackShellPlugin;
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@
},
"homepage": "https://github.com/1337programming/webpack-shell-plugin",
"devDependencies": {
"babel-core": "^6.7.6",
"babel-preset-es2015-rollup": "^1.1.1",
"babel-core": "^6.26.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-runtime": "^6.26.0",
"babelrc-rollup": "^3.0.0",
"css-loader": "^0.23.1",
"eslint": "^2.7.0",
"rollup": "^0.25.8",
"rollup-plugin-babel": "^2.4.0",
"polyfill": "^0.1.0",
"rollup": "^0.55.3",
"rollup-plugin-babel": "^3.0.3",
"style-loader": "^0.13.1",
"webpack": "^1.13.1"
}
Expand Down
Loading