Created
October 5, 2014 00:22
-
-
Save myndzi/0e6d842f573e4a0e3dfc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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