This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
installer.js
490 lines (461 loc) · 14.8 KB
/
installer.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
'use strict'
const BB = require('bluebird')
const {
linkBin: binLink,
extract,
getPrefix,
log: npmlog,
logicalTree: buildLogicalTree,
parseArg: npa,
readJSON: readPkgJson,
runScript,
verifyLock: lockVerify
} = require('libnpm')
const config = require('./config.js')
const ensurePackage = require('./ensure-package.js')
const path = require('path')
const ssri = require('ssri')
const fs = require('graceful-fs')
const glob = BB.promisify(require('glob'))
const mkdirp = BB.promisify(require('mkdirp'))
const rimraf = BB.promisify(require('rimraf'))
const { spawn } = require('child_process')
const readFileAsync = BB.promisify(fs.readFile)
const readdirAsync = BB.promisify(fs.readdir)
const realpathAsync = BB.promisify(fs.realpath)
const symlinkAsync = BB.promisify(fs.symlink)
const writeFileAsync = BB.promisify(fs.writeFile)
class Installer {
constructor (opts) {
this.opts = config(opts)
// Stats
this.startTime = Date.now()
this.runTime = 0
this.timings = { scripts: 0 }
this.pkgCount = 0
// Misc
this.log = (level, ...msgs) => {
this.opts.log[level](...msgs)
}
this.onlyDeps = opts.only && new Set(opts.only)
this.pkg = null
this.tree = null
this.validLockHash = false
this.force = this.opts.force
this.failedDeps = new Set()
}
async timedStage (name) {
const start = Date.now()
const ret = await this[name].apply(this, [].slice.call(arguments, 1))
this.timings[name] = Date.now() - start
this.log('info', name, `Done in ${this.timings[name] / 1000}s`)
return ret
}
async run () {
try {
await this.timedStage('prepare')
if (!this.validLockHash || this.force) {
this.log('info', 'Generating new package map')
await this.timedStage('fetchTree', this.tree)
await this.timedStage('updateJson', this.tree)
await this.timedStage('buildTree', this.tree)
await this.tinkifyBins()
await this.timedStage('runScript', 'prepublish', this.pkg, this.prefix)
await this.timedStage('runScript', 'prepare', this.pkg, this.prefix)
await this.timedStage('writeLockHash')
} else {
this.log('info', 'Found valid existing package map. Skipping fetch.')
}
await this.timedStage('teardown')
this.runTime = Date.now() - this.startTime
this.log(
'info',
'run-scripts',
`total script time: ${this.timings.scripts / 1000}s`
)
this.log(
'info',
'run-time',
`total run time: ${this.runTime / 1000}s`
)
if (this.pkgCount) {
this.log(
'info',
'package-count',
`total packages: ${this.pkgCount}`
)
}
} catch (err) {
if (err.message.match(/aggregate error/)) {
throw err[0]
} else {
throw err
}
} finally {
await this.timedStage('teardown')
}
this.opts = null
return this
}
async prepare () {
this.log('info', 'prepare', 'initializing installer')
const prefix = (
this.opts.prefix && this.opts.global
? this.opts.prefix
// There's some Special™ logic around the `--prefix` config when it
// comes from a config file or env vs when it comes from the CLI
: process.argv.some(arg => arg.match(/^\s*--prefix\s*/i))
? this.opts.prefix
: await getPrefix(process.cwd())
)
this.prefix = prefix
this.log('verbose', 'prepare', 'installation prefix: ' + prefix)
await BB.join(
readJson(prefix, 'package.json'),
readJson(prefix, 'package-lock.json', true),
readJson(prefix, 'npm-shrinkwrap.json', true),
readJson(prefix, 'node_modules/.pkglock-hash', true),
(pkg, lock, shrink, lockHash) => {
if (shrink) {
this.log('verbose', 'prepare', 'using npm-shrinkwrap.json')
} else if (lock) {
this.log('verbose', 'prepare', 'using package-lock.json')
}
pkg._shrinkwrap = shrink || lock
this.pkg = pkg
this.pkglockHash = lockHash
}
)
await this.checkLock()
this.tree = buildLogicalTree(this.pkg, this.pkg._shrinkwrap)
if (this.onlyDeps && this.onlyDeps.size) {
for (const [key, dep] of this.tree.dependencies.entries()) {
if (!this.onlyDeps.has(key)) {
this.tree.delDep(dep)
}
}
if (!this.tree.dependencies.size) {
throw new Error('No dependencies found matching filter')
}
}
this.log('silly', 'tree', this.tree)
this.expectedTotal = 0
this.tree.forEach((dep, next) => {
this.expectedTotal++
next()
})
}
async teardown () {
this.log('verbose', 'teardown', 'shutting down')
}
async checkLock () {
this.log('verbose', 'checkLock', 'verifying package-lock data')
const pkg = this.pkg
const prefix = this.prefix
if (
this.pkglockHash &&
!ssri.checkData(
JSON.stringify(pkg._shrinkwrap),
this.pkglockHash.lockfile_integrity
)
) {
this.validLockHash = false
}
if (!pkg._shrinkwrap || !pkg._shrinkwrap.lockfileVersion) {
this.log('warn', 'No lockfile detected. Regenerating.')
await this.npmInstall()
}
if (!pkg._shrinkwrap || !pkg._shrinkwrap.lockfileVersion) {
throw new Error(`npm install to generate package-lock.json failed. This is a bug.`)
}
const result = await lockVerify(prefix)
if (result.status) {
result.warnings.forEach(w => this.log('warn', 'lockfile', w))
} else {
this.log('warn', 'lockfile', 'some package-lock dependencies are not in sync with package.json.\n' + result.errors.join('\n'))
if (result.warnings.length) {
this.log('warn', 'lockfile', result.warnings.map(w => 'Warning: ' + w).join('\n'))
}
this.log('warn', 'lockfile', 'Updating package-lock.')
await this.npmInstall()
}
}
async npmInstall () {
await BB.fromNode(cb => {
const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm'
spawn(npmBin, [
'install',
'--package-lock',
'--package-lock-only'
], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
}).on('error', cb).on('close', code => cb(null, code))
})
this.pkg._shrinkwrap = await readJson(this.prefix, 'package-lock.json')
}
async fetchTree (tree) {
this.log('verbose', 'fetchTree', 'making sure all required deps are in the cache')
// const cg = this.log('newItem', 'fetchTree', this.expectedTotal)
await tree.forEachAsync(async (dep, next) => {
if (!this.checkDepEnv(dep)) { return }
const depPath = dep.path(this.prefix)
const spec = npa.resolve(dep.name, dep.version, this.prefix)
if (dep.isRoot) {
return next()
} else if (spec.type === 'directory') {
const relative = path.relative(path.dirname(depPath), spec.fetchSpec)
this.log('silly', 'fetchTree', `${dep.name}@${spec.fetchSpec} -> ${depPath} (symlink)`)
await mkdirp(path.dirname(depPath))
try {
await symlinkAsync(relative, depPath, 'junction')
} catch (e) {
await rimraf(depPath)
await symlinkAsync(relative, depPath, 'junction')
}
await next()
this.pkgCount++
// cg.completeWork(1)
} else {
this.log('silly', 'fetchTree', `${dep.name}@${dep.version} -> ${depPath}`)
if (dep.bundled) {
// cg.completeWork(1)
this.pkgCount++
await next()
} else {
dep.metadata = await ensurePackage(
this.opts.cache, dep.name, dep, this.opts.concat({
warn: msg => this.opts.log('warn', msg)
})
)
const pkg = await readJson(dep.path(this.prefix), 'package.json')
if (
(
pkg.scripts && (
pkg.scripts.preinstall ||
pkg.scripts.install ||
pkg.scripts.postinstall
)
) || (
pkg.bundleDependencies ||
pkg.bundledDependencies
)
) {
await extract(
npa.resolve(dep.name, dep.version),
dep.path(this.prefix),
this.opts.concat({
integrity: dep.integrity,
resolved: dep.resolved
})
)
}
// cg.completeWork(1)
this.pkgCount++
await next()
}
}
}, { concurrency: 50, Promise: BB })
// cg.finish()
}
checkDepEnv (dep) {
const includeDev = (
this.opts.dev ||
this.opts.development ||
(
!/^prod(uction)?$/.test(this.opts.only) &&
!this.opts.production
) ||
/^dev(elopment)?$/.test(this.opts.only) ||
/^dev(elopment)?$/.test(this.opts.also)
)
const includeProd = !/^dev(elopment)?$/.test(this.opts.only)
return (dep.dev && includeDev) || (!dep.dev && includeProd)
}
async updateJson (tree) {
this.log('verbose', 'updateJson', 'checking for native builds')
const pkgJsons = new Map()
await tree.forEachAsync(async (dep, next) => {
if (!this.checkDepEnv(dep)) { return }
const depPath = dep.path(this.prefix)
await next()
const pkg = await readJson(depPath, 'package.json')
await this.updateInstallScript(dep, pkg)
pkgJsons.set(dep, pkg)
}, { concurrency: 100, Promise: BB })
this.pkgJsons = pkgJsons
return pkgJsons
}
async updateInstallScript (dep, pkg) {
const depPath = dep.path(this.prefix)
if (!pkg.scripts || !pkg.scripts.install) {
const files = await readdirAsync(depPath)
if (files.find(f => /\.gyp$/i.test(f))) {
if (!pkg.scripts) {
pkg.scripts = {}
}
pkg.scripts.install = 'node-gyp rebuild'
}
}
let modified
if (pkg.scripts) {
if (pkg.scripts.preinstall) {
const old = pkg.scripts.preinstall
pkg.scripts.preinstall = pkg.scripts.preinstall.replace(/\bnode([^-\w]|$)/, 'tish$1')
modified = pkg.scripts.preinstall === old
}
if (pkg.scripts.install) {
const old = pkg.scripts.install
pkg.scripts.install = pkg.scripts.install.replace(/\bnode([^-\w]|$)/, 'tish$1')
modified = pkg.scripts.install === old
}
if (pkg.scripts.postinstall) {
const old = pkg.scripts.postinstall
pkg.scripts.postinstall = pkg.scripts.postinstall.replace(/\bnode([^-\w]|$)/, 'tish$1')
modified = pkg.scripts.postinstall === old
}
if (modified) {
await writeFileAsync(path.join(depPath, 'package.json'), JSON.stringify(pkg, null, 2))
}
}
return pkg
}
async buildTree (tree) {
this.log('verbose', 'buildTree', 'finalizing tree and running scripts')
await tree.forEachAsync(async (dep, next) => {
if (!this.checkDepEnv(dep)) { return }
try {
const spec = npa.resolve(dep.name, dep.version)
const depPath = dep.path(this.prefix)
this.log('silly', 'buildTree', `linking ${spec}`)
const pkg = this.pkgJsons.get(dep)
await this.runScript('preinstall', pkg, depPath)
await next() // build children between preinstall and binLink
// Don't link root bins
if (
dep.isRoot ||
!(pkg.bin || pkg.man || (pkg.directories && pkg.directories.bin) || (pkg.scripts && (pkg.scripts.install || pkg.scripts.postinstall)))
) {
// We skip the relatively expensive readPkgJson if there's no way
// we'll actually be linking any bins or mans
return
}
const pkgJson = await readPkgJson(path.join(depPath, 'package.json'))
await binLink(pkgJson, depPath, false, {
force: this.opts.force,
ignoreScripts: this.opts['ignore-scripts'],
log: npmlog,
name: pkg.name,
pkgId: pkg.name + '@' + pkg.version,
prefix: this.prefix,
prefixes: [this.prefix]
})
await this.runScript('install', pkg, depPath)
await this.runScript('postinstall', pkg, depPath)
} catch (e) {
if (dep.optional) {
this.failedDeps.add(dep)
} else {
throw e
}
}
}, { concurrency: 50, Promise: BB })
}
async tinkifyBins () {
const old = process.tink
process.tink = null
const bins = await glob(path.join(this.prefix, 'node_modules/**/.bin/*'))
process.tink = old
this.log('verbose', 'tinkifyBins', 'convering installed bins to use tink:', bins)
return BB.map(bins, async bin => {
const real = await realpathAsync(bin)
const data = (await readFileAsync(real, 'utf8')).replace(/^(#!.*\b)node([^-\w]|$)/g, '$1tish$2')
await writeFileAsync(real, data, 'utf8')
}, { concurrency: 50, Promise: BB })
}
// A cute little mark-and-sweep collector!
async garbageCollect (tree) {
if (!this.failedDeps.size) { return }
const purged = await sweep(
tree,
this.prefix,
mark(tree, this.failedDeps)
)
this.purgedDeps = purged
this.pkgCount -= purged.size
}
async runScript (stage, pkg, pkgPath) {
const start = Date.now()
if (!this.opts['ignore-scripts']) {
// TODO(mikesherov): remove pkg._id when npm-lifecycle no longer relies on it
pkg._id = pkg.name + '@' + pkg.version
const ret = await runScript(pkg, stage, pkgPath, {
dir: this.prefix,
log: npmlog,
config: this.opts
})
this.timings.scripts += Date.now() - start
return ret
}
}
async writeLockHash (map) {
const nm = path.join(this.prefix, 'node_modules')
try {
await mkdirp(nm)
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
return writeFileAsync(path.join(nm, '.pkglock-hash'), JSON.stringify({
lockfile_integrity: ssri.fromData(
JSON.stringify(this.pkg._shrinkwrap)
).toString()
}))
}
}
module.exports = treeFrog
async function treeFrog (opts) {
return new Installer(opts).run()
}
module.exports.Installer = Installer
function mark (tree, failed) {
const liveDeps = new Set()
tree.forEach((dep, next) => {
if (!failed.has(dep)) {
liveDeps.add(dep)
next()
}
})
return liveDeps
}
async function sweep (tree, prefix, liveDeps) {
const purged = new Set()
await tree.forEachAsync(async (dep, next) => {
await next()
if (
!dep.isRoot && // never purge root! 🙈
!liveDeps.has(dep) &&
!purged.has(dep)
) {
purged.add(dep)
await rimraf(dep.path(prefix))
}
}, { concurrency: 50, Promise: BB })
return purged
}
function stripBOM (str) {
return str.replace(/^\uFEFF/, '')
}
module.exports._readJson = readJson
async function readJson (jsonPath, name, ignoreMissing) {
try {
const str = await readFileAsync(path.join(jsonPath, name), 'utf8')
return JSON.parse(stripBOM(str))
} catch (err) {
if (err.code !== 'ENOENT' || !ignoreMissing) {
throw err
}
}
}