-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
66 lines (58 loc) · 1.43 KB
/
index.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
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
65
66
var spawn = require('child_process').spawn
var dargs = require('dargs')
var noop = function () {}
module.exports = command.bind(null, 'install')
module.exports.install = command.bind(null, 'install')
module.exports.uninstall = command.bind(null, 'uninstall')
function command (cmd, packages, opt, cb) {
if (typeof opt === 'function') {
cb = opt
opt = {}
} else {
opt = opt || {}
}
cb = cb || noop
if (typeof packages === 'function') {
throw new TypeError('expected packages as first argument')
}
// packages to install
var deps = [].concat(packages).filter(Boolean)
if (deps.length === 0) {
return process.nextTick(function () {
cb(null)
})
}
var npmCmd = opt.command
if (!npmCmd) {
npmCmd = process.platform === 'win32'
? 'npm.cmd'
: 'npm'
}
var spawnArgs = {
cwd: opt.cwd,
env: opt.env || process.env,
stdio: opt.stdio
}
if (opt.shell) spawnArgs.shell = opt.shell;
var cliArgs = dargs(opt, {
excludes: Object.keys(spawnArgs)
})
var args = [cmd].concat(deps).concat(cliArgs)
var proc = spawn(npmCmd, args, spawnArgs)
var error = ''
if (proc.stderr) {
proc.stderr.on('data', function (data) {
error += data.toString()
})
}
proc.once('exit', function (code) {
// ensure we pass an Error
if (code === 0) {
cb(null)
} else {
var msg = error || 'exit code ' + code
cb(new Error(msg))
}
})
return proc
}