-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
98 lines (80 loc) · 2.41 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict'
var npmPath = require('npm-path')
var childProcess = require('child_process')
var exec = childProcess.exec
var spawn = childProcess.spawn
var spawnSync = childProcess.spawnSync
// polyfill for childProcess.execSync
var execSync = childProcess.execSync
npmExec.spawn = npmSpawn
npmExec.spawnSync = npmSpawnSync
npmExec.sync = npmExecSync
npmExec.exec = npmExec
npmExec.execSync = npmExecSync
module.exports = npmExec
function npmExec (command, options, fn) {
var a = normalizeExecArgs(command, options, fn)
command = a[0]
options = a[1]
fn = a[2]
return exec(command, augmentOptionsSync(options), fn)
}
function npmSpawn (command, args, options, fn) {
var a = normalizeSpawnArgs(command, args, options, fn)
command = a[0]
args = a[1]
options = a[2]
fn = a[3]
return spawn(command, args, augmentOptionsSync(options), fn)
}
function npmSpawnSync (command, args, options) {
var a = normalizeSpawnArgs(command, args, options)
command = a[0]
args = a[1]
options = a[2]
return spawnSync(command, args, augmentOptionsSync(options))
}
function npmExecSync (command, options) {
var a = normalizeExecArgs(command, options)
command = a[0]
options = a[1]
return execSync(command, augmentOptionsSync(options))
}
function augmentOptionsSync (options) {
options = options || {}
var newPath = npmPath.getSync(options)
var env = Object.create(options.env)
env[npmPath.PATH] = newPath
options.env = env
return options
}
function normalizeSpawnArgs (file /*, args, options */) {
var args, options
if (Array.isArray(arguments[1])) {
args = arguments[1].slice(0)
options = arguments[2]
} else if (arguments[1] !== undefined &&
(arguments[1] === null || typeof arguments[1] !== 'object')) {
throw new TypeError('Incorrect value of args option')
} else {
args = []
options = arguments[1]
}
if (options === undefined) { options = {} } else if (options === null || typeof options !== 'object') { throw new TypeError('options argument must be an object') }
return [file, args, options]
}
function normalizeExecArgs (command /*, options, callback */) {
var options, callback
if (typeof arguments[1] === 'function') {
options = undefined
callback = arguments[1]
} else {
options = arguments[1]
callback = arguments[2]
}
callback = callback || noopErr
return [command, options, callback]
}
function noopErr (err) {
if (err) throw err
}