diff options
author | Robert Kowalski <rok@kowalski.gd> | 2013-04-27 16:28:05 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-04-29 08:06:52 +0400 |
commit | 8e879f558e15dae25ee81da7d73164896050f8ea (patch) | |
tree | 950397ae61c0e13d845eb25c54d4e0ca2cfd1163 /lib | |
parent | b41fc465280fcc79109094f4eae1ff719a0e86c8 (diff) |
remove exec.js, use child_process.execFile
Fixes #3312
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bugs.js | 3 | ||||
-rw-r--r-- | lib/cache.js | 22 | ||||
-rw-r--r-- | lib/config.js | 5 | ||||
-rw-r--r-- | lib/docs.js | 3 | ||||
-rw-r--r-- | lib/edit.js | 6 | ||||
-rw-r--r-- | lib/explore.js | 8 | ||||
-rw-r--r-- | lib/help.js | 8 | ||||
-rw-r--r-- | lib/rebuild.js | 1 | ||||
-rw-r--r-- | lib/submodule.js | 22 | ||||
-rw-r--r-- | lib/utils/exec.js | 85 | ||||
-rw-r--r-- | lib/utils/lifecycle.js | 13 | ||||
-rw-r--r-- | lib/version.js | 19 |
12 files changed, 61 insertions, 134 deletions
diff --git a/lib/bugs.js b/lib/bugs.js index fa66dc3e4..bcbf2bebb 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -3,8 +3,7 @@ module.exports = bugs bugs.usage = "npm bugs <pkgname>" -var exec = require("./utils/exec.js") - , npm = require("./npm.js") +var npm = require("./npm.js") , registry = npm.registry , log = require("npmlog") , opener = require("opener") diff --git a/lib/cache.js b/lib/cache.js index 200744224..33fea0723 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -58,8 +58,8 @@ cache.lock = lock cache.unlock = unlock var mkdir = require("mkdirp") - , exec = require("./utils/exec.js") , spawn = require("child_process").spawn + , exec = require("child_process").execFile , once = require("once") , fetch = require("./utils/fetch.js") , npm = require("./npm.js") @@ -428,10 +428,10 @@ function checkGitDir (p, u, co, origUrl, cb) { }) var git = npm.config.get("git") - var args = ["config", "--get", "remote.origin.url"] + var args = [ "config", "--get", "remote.origin.url" ] var env = gitEnv() - exec(git, args, env, false, p, function (er, code, stdout, stderr) { + exec(git, args, {cwd: p, env: env}, function (er, stdout, stderr) { stdoutTrimmed = (stdout + "\n" + stderr).trim() if (er || u !== stdout.trim()) { log.warn( "`git config --get remote.origin.url` returned " @@ -450,8 +450,12 @@ function checkGitDir (p, u, co, origUrl, cb) { function cloneGitRemote (p, u, co, origUrl, cb) { mkdir(p, function (er) { if (er) return cb(er) - exec( npm.config.get("git"), ["clone", "--mirror", u, p], gitEnv(), false - , function (er, code, stdout, stderr) { + + var git = npm.config.get("git") + var args = [ "clone", "--mirror", u, p ] + var env = gitEnv() + + exec(git, args, {cwd: p, env: env}, function (er, stdout, stderr) { stdout = (stdout + "\n" + stderr).trim() if (er) { log.error("git clone " + u, stdout) @@ -465,8 +469,8 @@ function cloneGitRemote (p, u, co, origUrl, cb) { function archiveGitRemote (p, u, co, origUrl, cb) { var git = npm.config.get("git") - var archive = ["fetch", "-a", "origin"] - var resolve = ["rev-list", "-n1", co] + var archive = [ "fetch", "-a", "origin" ] + var resolve = [ "rev-list", "-n1", co ] var env = gitEnv() var errState = null @@ -474,7 +478,7 @@ function archiveGitRemote (p, u, co, origUrl, cb) { var resolved = null var tmp - exec(git, archive, env, false, p, function (er, code, stdout, stderr) { + exec(git, archive, {cwd: p, env: env}, function (er, stdout, stderr) { stdout = (stdout + "\n" + stderr).trim() if (er) { log.error("git fetch -a origin ("+u+")", stdout) @@ -486,7 +490,7 @@ function archiveGitRemote (p, u, co, origUrl, cb) { }) function resolveHead () { - exec(git, resolve, env, false, p, function (er, code, stdout, stderr) { + exec(git, resolve, {cwd: p, env: env}, function (er, stdout, stderr) { stdout = (stdout + "\n" + stderr).trim() if (er) { log.error("Failed resolving git HEAD (" + u + ")", stderr) diff --git a/lib/config.js b/lib/config.js index 27a5f3546..e28285932 100644 --- a/lib/config.js +++ b/lib/config.js @@ -11,7 +11,7 @@ config.usage = "npm config set <key> <value>" var log = require("npmlog") , npm = require("./npm.js") - , exec = require("./utils/exec.js") + , spawn = require("child_process").spawn , fs = require("graceful-fs") , npmconf = require("npmconf") , types = npmconf.defs.types @@ -93,7 +93,8 @@ function edit (cb) { , "utf8" , function (er) { if (er) return cb(er) - exec(e, [f], cb) + var editor = spawn(e, [f], {env: process.env, stdio: "inherit"}) + editor.on("close", cb) } ) }) diff --git a/lib/docs.js b/lib/docs.js index ffa91aa6b..ff2e381f8 100644 --- a/lib/docs.js +++ b/lib/docs.js @@ -10,8 +10,7 @@ docs.completion = function (opts, cb) { }) } -var exec = require("./utils/exec.js") - , npm = require("./npm.js") +var npm = require("./npm.js") , registry = npm.registry , log = require("npmlog") , opener = require("opener") diff --git a/lib/edit.js b/lib/edit.js index 0ffe723bc..fe30136c5 100644 --- a/lib/edit.js +++ b/lib/edit.js @@ -7,7 +7,7 @@ edit.usage = "npm edit <pkg>" edit.completion = require("./utils/completion/installed-shallow.js") var npm = require("./npm.js") - , exec = require("./utils/exec.js") + , spawn = require("child_process").spawn , path = require("path") , fs = require("graceful-fs") @@ -22,7 +22,9 @@ function edit (args, cb) { .replace(/(\/node_modules)+/, "/node_modules") fs.lstat(path.resolve(npm.dir, p), function (er) { if (er) return cb(er) - exec(editor, [path.resolve(npm.dir, p)], function (er) { + + var ed = spawn(editor, [path.resolve(npm.dir, p)], {stdio: "inherit"}) + ed.on("close", function (er) { if (er) return cb(er) npm.commands.rebuild(args, cb) }) diff --git a/lib/explore.js b/lib/explore.js index 8392c9172..f52dc1dee 100644 --- a/lib/explore.js +++ b/lib/explore.js @@ -1,4 +1,4 @@ -// npm expore <pkg>[@<version>] +// npm explore <pkg>[@<version>] // open a subshell to the package folder. module.exports = explore @@ -6,7 +6,7 @@ explore.usage = "npm explore <pkg> [ -- <cmd>]" explore.completion = require("./utils/completion/installed-shallow.js") var npm = require("./npm.js") - , exec = require("./utils/exec.js") + , spawn = require("child_process").spawn , path = require("path") , fs = require("graceful-fs") @@ -25,7 +25,9 @@ function explore (args, cb) { if (!args.length) console.log( "\nExploring "+cwd+"\n"+ "Type 'exit' or ^D when finished\n") - exec(npm.config.get("shell"), args, null, true, cwd, function (er) { + + var shell = spawn(npm.config.get("shell"), args, {cwd: cwd, stdio: "inherit"}) + shell.on("close", function (er) { // only fail if non-interactive. if (!args.length) return cb() cb(er) diff --git a/lib/help.js b/lib/help.js index a8b1f2d4d..7a9f5ecd6 100644 --- a/lib/help.js +++ b/lib/help.js @@ -10,7 +10,7 @@ help.completion = function (opts, cb) { var fs = require("graceful-fs") , path = require("path") - , exec = require("./utils/exec.js") + , spawn = require("child_process").spawn , npm = require("./npm.js") , log = require("npmlog") , opener = require("opener") @@ -60,7 +60,8 @@ function help (args, cb) { switch (viewer) { case "woman": var a = ["-e", "(woman-find-file \"" + sectionPath + "\")"] - exec("emacsclient", a, env, true, cb) + var woman = spawn("emacsclient", a, {env: env, stdio: "inherit"}) + woman.on("close", cb) break case "browser": @@ -68,7 +69,8 @@ function help (args, cb) { break default: - exec("man", [num, section], env, true, cb) + var man = spawn("man", [num, section], {env: env, stdio: "inherit"}) + man.on("close", cb) } } ) diff --git a/lib/rebuild.js b/lib/rebuild.js index 0c97cf85d..35bb454d7 100644 --- a/lib/rebuild.js +++ b/lib/rebuild.js @@ -8,7 +8,6 @@ var readInstalled = require("read-installed") , npm = require("./npm.js") , asyncMap = require("slide").asyncMap , fs = require("graceful-fs") - , exec = require("./utils/exec.js") rebuild.usage = "npm rebuild [<name>[@<version>] [name[@<version>] ...]]" diff --git a/lib/submodule.js b/lib/submodule.js index c1523984c..0c94281b7 100644 --- a/lib/submodule.js +++ b/lib/submodule.js @@ -5,7 +5,7 @@ module.exports = submodule var npm = require("./npm.js") - , exec = require("./utils/exec.js") + , exec = require("child_process").execFile , cache = require("./cache.js") , asyncMap = require("slide").asyncMap , chain = require("slide").chain @@ -55,15 +55,17 @@ function submodule_ (pkg, cb) { } function updateSubmodule (name, cb) { - exec( npm.config.get("git"), [ "submodule", "update", "--init" - , "node_modules/" + name ] - , null, true, npm.prefix, cb) + var git = npm.config.get("git") + var args = [ "submodule", "update", "--init", "node_modules/", name ] + + exec(git, args, cb) } function addSubmodule (name, url, cb) { - exec( npm.config.get("git"), [ "submodule", "add", url - , "node_modules/" + name ] - , null, true, npm.prefix, function (er) { + var git = npm.config.get("git") + var args = [ "submodule", "add", url, "node_modules/", name ] + + exec(git, args, function (er) { if (er) return cb(er) updateSubmodule(name, cb) }) @@ -71,8 +73,10 @@ function addSubmodule (name, url, cb) { var getSubmodules = function getSubmodules (cb) { - exec( npm.config.get("git"), ["submodule", "status"], null, false - , npm.prefix, function (er, code, stdout, stderr) { + var git = npm.config.get("git") + var arg = [ "submodule", "status" ] + + exec(git, args, function (er, stdout, stderr) { if (er) return cb(er) res = stdout.trim().split(/\n/).map(function (line) { return line.trim().split(/\s+/)[1] diff --git a/lib/utils/exec.js b/lib/utils/exec.js deleted file mode 100644 index 360367cbf..000000000 --- a/lib/utils/exec.js +++ /dev/null @@ -1,85 +0,0 @@ -module.exports = exec -exec.spawn = spawn - -var log = require("npmlog") - , child_process = require("child_process") - , util = require("util") - , npm = require("../npm.js") - , myUID = process.getuid ? process.getuid() : null - , myGID = process.getgid ? process.getgid() : null - , isRoot = process.getuid && myUID === 0 - , constants = require("constants") - , uidNumber = require("uid-number") - , once = require("once") - -function exec (cmd, args, env, takeOver, cwd, uid, gid, cb) { - if (typeof cb !== "function") cb = gid, gid = null - if (typeof cb !== "function") cb = uid, uid = null - if (typeof cb !== "function") cb = cwd, cwd = null - if (typeof cb !== "function") cb = takeOver, takeOver = true - if (typeof cb !== "function") cb = env, env = process.env - gid = gid == null ? myGID : gid - uid = uid == null ? myUID : uid - if (!isRoot) { - if (npm.config.get("unsafe-perm")) { - uid = myUID - gid = myGID - } else if (uid !== myUID || gid !== myGID) { - var e = new Error("EPERM: setuid() operation not permitted") - e.errno = constants.EPERM - return cb(e) - } - } - if (uid !== myUID) { - log.verbose("set uid", "from=%s to=%s", myUID, uid) - } - - if (uid && gid && (isNaN(uid) || isNaN(gid))) { - // get the numeric values - return uidNumber(uid, gid, function (er, uid, gid) { - if (er) return cb(er) - exec(cmd, args, env, takeOver, cwd, uid, gid, cb) - }) - } - - log.silly("exec", cmd+" "+args.map(JSON.stringify).join(" ")) - var stdout = "" - , stderr = "" - , cp = spawn(cmd, args, env, takeOver, cwd, uid, gid) - cb = once(cb) - cp.on("error", cb) - 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("close", function (code) { - var er = null - if (code) er = new Error("`"+cmd - +(args.length ? " " - + args.map(JSON.stringify).join(" ") - : "") - +"` failed with "+code) - cb(er, code, stdout, stderr) - }) - return cp -} - - -function spawn (c, a, env, takeOver, cwd, uid, gid) { - var fds = [ 0, 1, 2 ] - , opts = { customFds : takeOver ? fds : [-1,-1,-1] - , env : env || process.env - , cwd : cwd || null } - , cp - - if (uid && !isNaN(uid)) opts.uid = +uid - if (gid && !isNaN(gid)) opts.gid = +gid - - var name = c +" "+ a.map(JSON.stringify).join(" ") - log.silly([c, a, opts.cwd], "spawning") - cp = child_process.spawn(c, a, opts) - cp.name = name - return cp -} diff --git a/lib/utils/lifecycle.js b/lib/utils/lifecycle.js index 3f0042f81..d43e5ac88 100644 --- a/lib/utils/lifecycle.js +++ b/lib/utils/lifecycle.js @@ -3,7 +3,7 @@ exports = module.exports = lifecycle exports.cmd = cmd var log = require("npmlog") - , exec = require("./exec.js") + , spawn = require("child_process").spawn , npm = require("../npm.js") , path = require("path") , fs = require("graceful-fs") @@ -149,9 +149,9 @@ function runPackageLifecycle (pkg, env, wd, unsafe, cb) { + "\n> " + cmd + "\n" console.log(note) - exec( sh, [shFlag, cmd], env, true, wd - , user, group - , function (er, code, stdout, stderr) { + + var proc = spawn(sh, [shFlag, cmd], {cwd: wd, env: env, stdio: "inherit"}) + proc.on("close", function (er, stdout, stderr) { if (er && !npm.ROLLBACK) { log.info(pkg._id, "Failed to exec "+stage+" script") er.message = pkg._id + " " @@ -185,9 +185,8 @@ function runHookLifecycle (pkg, env, wd, unsafe, cb) { fs.stat(hook, function (er) { if (er) return cb() - exec( "sh", ["-c", cmd], env, true, wd - , user, group - , function (er) { + var proc = spawn("sh", ["-c", cmd], {cwd: wd, env: env, stdio: "inherit"}) + proc.on("close", function (er) { if (er) { er.message += "\nFailed to exec "+stage+" hook script" log.info(pkg._id, er) diff --git a/lib/version.js b/lib/version.js index fb00e21d3..973675190 100644 --- a/lib/version.js +++ b/lib/version.js @@ -2,7 +2,7 @@ module.exports = version -var exec = require("./utils/exec.js") +var exec = require("child_process").execFile , semver = require("semver") , path = require("path") , fs = require("graceful-fs") @@ -72,8 +72,11 @@ function version (args, silent, cb_) { } function checkGit (data, cb) { - exec( npm.config.get("git"), ["status", "--porcelain"], process.env, false - , function (er, code, stdout, stderr) { + var git = npm.config.get("git") + var args = [ "status", "--porcelain" ] + var env = process.env + + exec(git, args, {env: env}, function (er, stdout, stderr) { var lines = stdout.trim().split("\n").filter(function (line) { return line.trim() && !line.match(/^\?\? /) }).map(function (line) { @@ -87,12 +90,10 @@ function checkGit (data, cb) { , sign = npm.config.get("sign-git-tag") , flag = sign ? "-sm" : "-am" chain - ( [ [ exec, npm.config.get("git") - , ["add","package.json"], process.env, false ] - , [ exec, npm.config.get("git") - , ["commit", "-m", message ], process.env, false ] - , [ exec, npm.config.get("git") - , ["tag", "v"+data.version, flag, message], process.env, false ] ] + ( [ [ exec, git, [ "add", "package.json" ], {env: process.env} ] + , [ exec, git, [ "commit", "-m", message ], {env: process.env} ] + , [ exec, git, [ "tag", "v" + data.version, flag, message ] + , {env: process.env} ] ] , cb ) }) }) |