Skip to content

Instantly share code, notes, and snippets.

@myndzi
Created October 5, 2014 00:22
Show Gist options
  • Save myndzi/0e6d842f573e4a0e3dfc to your computer and use it in GitHub Desktop.
Save myndzi/0e6d842f573e4a0e3dfc to your computer and use it in GitHub Desktop.
'use strict';
var child_process = require('child_process');
var Promise = require('bluebird');
module.exports = Promise.method(function spawn(command, args, cwd) {
if (!command || !cwd) {
throw new Error("Both command and working directory must be given, not " + command + " and " + cwd);
}
if (args && !args.every(function (arg) {
var type = typeof arg;
return type === "boolean" || type === "string" || type === "number";
})) {
throw new Error("All arguments must be a boolean, string or number");
}
return new Promise(function (resolve, reject) {
var proc = child_process.spawn(command, args, {
cwd: cwd,
stdio: 'inherit'
});
proc.on("error", reject);
proc.on("exit", function(code) {
if (code !== 0) {
var e = new Error('Non-zero exit code: ' + code);
e.code = code;
reject(e);
} else {
resolve();
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment