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

exec.js « utils « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d7f7108b666e9b8c183650eccc36fe896b9bfb9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

module.exports = exec
exec.spawn = spawn
exec.pipe = pipe

var log = require("../utils/log")
  , child_process = require("child_process")
  , sys = require("sys")

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 stdout = ""
    , stderr = ""
    , cp = spawn(cmd, args, env, takeOver)
  cp.stdout && cp.stdout.on("data", function (chunk) {
    if (chunk) stdout += chunk
  })
  cp.stderr && cp.stderr.on("data", function (chunk) {
    if (chunk) stderr += chunk
  })
  cp.on("exit", function (code) {
    if (code) cb(new Error("`"+cmd+"` failed with "+code))
    else cb(null, code, stdout, stderr)
  })
  return cp
}

function logger (d) { if (d) process.binding("stdio").writeError(d+"") }
function pipe (cp1, cp2, cb) {
  sys.pump(cp1.stdout, cp2.stdin)
  var errState = null
  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(errState = new Error(
      "Failed "+(cp1.name || "<unknown>")+"\nexited with "+code))
  })
  cp2.on("exit", function (code) {
    if (errState) return
    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
}