1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-05-02 20:37:08 +01:00

Improve proc spawn behavior.

Interim until async / promises support is in.
This commit is contained in:
hacksalot
2016-02-01 09:25:22 -05:00
parent 36d641801b
commit 212b01092c
8 changed files with 75 additions and 95 deletions

View File

@ -5,41 +5,39 @@ Safe spawn utility for HackMyResume / FluentCV.
@license MIT. See LICENSE.md for details.
*/
/** Safely spawn a process synchronously or asynchronously without throwing an
exception
*/
(function() {
module.exports = function(cmd, args, isSync, callback) {
module.exports = function(cmd, args, isSync, callback, param) {
var info, spawn;
try {
spawn = require('child_process')[isSync ? 'spawnSync' : 'spawn'];
info = spawn(cmd, args);
if (!isSync) {
info.on('error', function(err) {
if (callback != null) {
callback(err);
} else {
throw {
cmd: cmd,
inner: err
};
if (typeof callback === "function") {
callback(err, param);
}
});
} else {
if (info.error) {
if (callback != null) {
callback(err);
} else {
throw {
cmd: cmd,
inner: info.error
};
if (typeof callback === "function") {
callback(info.error, param);
}
return {
cmd: cmd,
inner: info.error
};
}
}
} catch (_error) {
if (callback != null) {
return callback(_error);
} else {
throw _error;
if (typeof callback === "function") {
callback(_error, param);
}
return _error;
}
};