diff options
Diffstat (limited to 'lib/utils/exec.js')
-rw-r--r-- | lib/utils/exec.js | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/lib/utils/exec.js b/lib/utils/exec.js index 5ef070950..5bdc512f2 100644 --- a/lib/utils/exec.js +++ b/lib/utils/exec.js @@ -1,22 +1,19 @@ +module.exports = exec +exec.spawn = spawn +exec.pipe = pipe + var log = require("../utils/log") + , child_process = require("child_process") + , sys = require("sys") -module.exports = function exec (cmd, args, env, pipe, cb) { - if (!cb) cb = pipe, pipe = false +function exec (cmd, args, env, takeOver, cb) { + if (!cb) cb = takeOver, takeOver = true if (!cb) cb = env, env = process.env log.verbose(cmd+" "+args.map(JSON.stringify).join(" "), "exec") - var stdio = process.binding("stdio") - , fds = [ stdio.stdinFD || 0 - , stdio.stdoutFD || 1 - , stdio.stderrFD || 2 - ] - , cp = require("child_process").spawn( cmd - , args - , env - , pipe ? null : fds - ) - , stdout = "" + var stdout = "" , stderr = "" + , cp = spawn(cmd, args, env, takeOver) cp.stdout && cp.stdout.on("data", function (chunk) { if (chunk) stdout += chunk }) @@ -29,3 +26,36 @@ module.exports = function exec (cmd, args, env, pipe, cb) { }) return cp } + +function logger (d) { if (d) process.binding("stdio").writeError(d+"") } +function pipe (cp1, cp2, cb) { + sys.pump(cp1.stdout, cp2.stdin) + if (log.level <= log.LEVEL.silly) { + cp1.stderr.on("data", logger) + cp2.stderr.on("data", logger) + } + cp1.on("exit", function (code) { + if (!code) return log.verbose(cp2.name || "<unknown>", "success") + cp2.kill() + cb(new Error( "Failed "+(cp1.name || "<unknown>")+"\nexited with "+code)) + }) + cp2.on("exit", function (code) { + if (!code) return log.verbose(cp1.name || "<unknown>", "success", cb) + cb(new Error( "Failed "+(cp2.name || "<unknown>")+"\nexited with "+code)) + }) +} + +function spawn (c, a, env, takeOver) { + var stdio = process.binding("stdio") + , fds = [ stdio.stdinFD || 0 + , stdio.stdoutFD || 1 + , stdio.stderrFD || 2 + ] + , cp = child_process.spawn( c + , a + , env + , takeOver ? fds : null + ) + cp.name = c +" "+ a.map(JSON.stringify).join(" ") + return cp +} |