Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2010-08-25 13:58:05 +0400
committerisaacs <i@izs.me>2010-08-25 16:21:53 +0400
commita9a05c5d6c8987d9192b9f7f755d8f48db709eaf (patch)
treee174d39d870321219753465c140ac9d55ba8c7a7
parent2e7fb6c7cc049cd11b02f369182f3ca2e1eed01c (diff)
Rework how exec works, and add pipe/spawn
-rw-r--r--lib/utils/exec.js56
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
+}