/
exec.js
27 lines (22 loc) · 827 Bytes
/
exec.js
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
module.exports = exec
var npm = require("./npm.js")
, output = require("./utils/output.js")
, cp = require("child_process")
, os = require("os")
exec.usage = "npm exec <command>\n(execute command using local package bin)"
exec.completion = require("./utils/completion/local-bin.js")
function exec (args, cb) {
var b = npm.bin, cmd
if (args.length == 0) return cb("Usage:\n"+exec.usage)
var cmd;
if (process.platform.match(/win/))
cmd = "SET OLDPATH=%PATH% & SET PATH=" + b + ";%PATH% & " +
args.join(" ") + " & SET PATH=%OLDPATH%"
else
cmd = "PATH=" + b + ":$PATH " + args.join(" ")
cp.exec(cmd, function (error, stdout, stderr) {
if (error !== null) return cb(stderr.replace(/\n$/, ''))
var o = stdout.replace(/\n$/, '')
output.write(o, function (er) { cb(er, o) })
})
}