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

npm-script-runner.js « bin - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf2e8147f09dc74128a7143596aa9da82b447d46 (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
#!/usr/bin/env node

// Run a command as a specific user.
// env is passed through untouched.
// usage: npm-script-runner $folder $uid $gid "make test"
// This is internal plumbing

var argv = process.argv.slice(2)
  , cwd = argv.shift()
  , uid = argv.shift()
  , gid = argv.shift()
  , cmd = argv.shift()
  , stdio = process.binding("stdio")
  , cp = require("child_process")

if (!isNaN(uid)) uid = +uid
if (!isNaN(gid)) gid = +gid

if (!uid || !gid || uid === "root") throw new Error(
  "Please set a non-zero/non-root uid and gid")

console.error("uid=%s gid=%s euid=%s egid=%s"
             , process.getuid(), process.getgid(), uid, gid)
console.error("cmd=%s", cmd)
process.setgid(gid)
process.setuid(uid)

cp.spawn( "sh", ["-c", cmd]
        , { env : process.env
          , cwd : cwd
          , customFds: [ stdio.stdinFD
                       , stdio.stdoutFD
                       , stdio.stderrFD ]
          } )
  .on("exit", function (c) { process.exit(c) })