-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (173 loc) · 4.35 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
module.exports = exec
var PATH = 'PATH'
var path = require('path')
var run = require('child_process').exec
var fs = require('fs')
var which = require('which')
if (process.platform === 'win32') {
PATH = 'Path'
Object.keys(process.env).forEach(function (e) {
if (e.match(/^PATH$/i)) {
PATH = e
}
})
}
function exec(stage, args, pkg, wd, cb) {
if (typeof cb !== 'function') {
cb = wd
wd = process.cwd()
}
if (typeof cb !== 'function') {
cb = pkg
pkg = {
scripts: {}
}
}
if(typeof cb !== 'function') {
cb = args
args = []
}
if (typeof cb !== 'function') {
cb = stage
stage = null
}
var env = makeEnv(pkg)
env.npm_lifecycle_event = stage
env.npm_node_execpath = env.NODE = env.NODE || process.execPath
env.npm_execpath = require.main.filename
var pathArr = []
var p = wd.split(/[\\\/]node_modules[\\\/]/)
var acc = path.resolve(p.shift())
p.forEach(function (pp) {
pathArr.unshift(path.join(acc, 'node_modules', '.bin'))
acc = path.join(acc, 'node_modules', pp)
})
pathArr.unshift(path.join(acc, 'node_modules', '.bin'))
// we also unshift the bundled node-gyp-bin folder so that
// the bundled one will be used for installing things.
pathArr.unshift(path.join(__dirname, '..', '..', 'bin', 'node-gyp-bin'))
if (shouldPrependCurrentNodeDirToPATH()) {
// prefer current node interpreter in child scripts
pathArr.push(path.dirname(process.execPath))
}
if (env[PATH]) pathArr.push(env[PATH])
env[PATH] = pathArr.join(process.platform === 'win32' ? ';' : ':')
env.npm_lifecycle_script = pkg.scripts[stage]
if (pkg.scripts[stage]) {
runCmd(pkg.scripts[stage], args, env, wd, cb)
} else {
fs.stat(path.join(wd, 'node_modules', '.bin', stage), function (er, stat) {
if (er || !stat.isFile()) {
return cb(new Error("Could not find script: " + stage))
}
return runCmd(path.resolve(path.join(wd, 'node_modules', '.bin', stage)), args, env, wd, cb)
})
}
}
function makeEnv(data, prefix, env) {
prefix = prefix || 'npm_package_'
if (!env) {
env = {}
for (var i in process.env) {
if (!i.match(/^npm_/)) {
env[i] = process.env[i]
}
}
} else if (!data.hasOwnProperty('_lifecycleEnv')) {
Object.defineProperty(data, '_lifecycleEnv', {
value: env,
enumerable: false
})
}
for (i in data) {
if (i.charAt(0) !== '_') {
var envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
if (i === 'readme') {
continue
}
if (data[i] && typeof data[i] === 'object') {
try {
// quick and dirty detection for cyclical structures
JSON.stringify(data[i])
makeEnv(data[i], envKey + '_', env)
} catch (ex) {
// usually these are package objects.
// just get the path and basic details.
var d = data[i]
makeEnv({
name: d.name,
version: d.version,
path: d.path
},
envKey + '_',
env
)
}
} else {
env[envKey] = String(data[i])
env[envKey] = env[envKey].indexOf('\n') !== -1 ?
JSON.stringify(env[envKey]) :
env[envKey]
}
}
}
if (prefix !== 'npm_package_') return env
var pkgConfig = data.config || {}
prefix = 'npm_package_config_'
for (var i in pkgConfig) {
var envKey = (prefix + i)
env[envKey] = pkgConfig[i]
}
return env
}
function shouldPrependCurrentNodeDirToPATH() {
var isWindows = process.platform === 'win32'
try {
var foundExecPath = which.sync(path.basename(process.execPath), {
pathExt: isWindows ? ';' : ':'
})
return process.execPath.toUpperCase() !== foundExecPath.toUpperCase()
} catch (e) {
return true
}
}
function runCmd(cmd, args, env, wd, cb_) {
function cb() {
cb_.apply(null, arguments)
}
var conf = {
cwd: wd,
env: env,
stdio: [0, 1, 2],
shell: which.sync('sh')
}
var proc = run(cmd + ' ' + args.join(' '), conf)
proc.on('error', procError)
proc.on('close', function (code, signal) {
if (signal) {
process.kill(process.pid, signal)
} else if (code) {
var er = new Error('Exit status ' + code)
}
procError(er)
})
process.stdin.pipe(proc.stdin)
proc.stdout.pipe(process.stdout)
proc.stderr.pipe(process.stderr)
process.once('SIGTERM', procKill)
function procError(er) {
if (er) {
er.message = '`' + cmd + args.join(' ') + '`\n' +
er.message
if (er.code !== 'EPERM') {
er.code = 'ELIFECYCLE'
}
er.script = cmd
}
process.removeListener('SIGTERM', procKill)
return cb(er)
}
function procKill() {
proc.kill()
}
}