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: e91562bdb71e6b87f836e952a633a36bae73e85e (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

var log = require("../utils/log")

module.exports = function exec (cmd, args, env, pipe, cb) {
  if (!cb) cb = pipe, pipe = false
  if (!cb) cb = env, env = process.env
  log(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 : env
                                           , customFDs : pipe ? null : fds
                                           }
                                         )
    , stdout = ""
    , stderr = ""
  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
}