-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathpackage-install-scripts.js
373 lines (331 loc) · 11.3 KB
/
package-install-scripts.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
/* @flow */
import type {Manifest} from './types.js';
import type PackageResolver from './package-resolver.js';
import type {Reporter} from './reporters/index.js';
import Config from './config.js';
import type {ReporterSetSpinner} from './reporters/types.js';
import executeLifecycleScript from './util/execute-lifecycle-script.js';
import * as crypto from './util/crypto.js';
import * as fsUtil from './util/fs.js';
import {getPlatformSpecificPackageFilename} from './util/package-name-utils.js';
import {packWithIgnoreAndHeaders} from './cli/commands/pack.js';
const fs = require('fs');
const invariant = require('invariant');
const path = require('path');
const INSTALL_STAGES = ['preinstall', 'install', 'postinstall'];
export type InstallArtifacts = {
[pattern: string]: Array<string>,
};
export default class PackageInstallScripts {
constructor(config: Config, resolver: PackageResolver, force: boolean) {
this.installed = 0;
this.resolver = resolver;
this.reporter = config.reporter;
this.config = config;
this.force = force;
this.artifacts = {};
}
needsPermission: boolean;
resolver: PackageResolver;
reporter: Reporter;
installed: number;
config: Config;
force: boolean;
artifacts: InstallArtifacts;
setForce(force: boolean) {
this.force = force;
}
setArtifacts(artifacts: InstallArtifacts) {
this.artifacts = artifacts;
}
getArtifacts(): InstallArtifacts {
return this.artifacts;
}
getInstallCommands(pkg: Manifest): Array<[string, string]> {
const scripts = pkg.scripts;
if (scripts) {
const cmds = [];
for (const stage of INSTALL_STAGES) {
const cmd = scripts[stage];
if (cmd) {
cmds.push([stage, cmd]);
}
}
return cmds;
} else {
return [];
}
}
async walk(loc: string): Promise<Map<string, number>> {
const files = await fsUtil.walk(loc, null, new Set(this.config.registryFolders));
const mtimes = new Map();
for (const file of files) {
mtimes.set(file.relative, file.mtime);
}
return mtimes;
}
async saveBuildArtifacts(
loc: string,
pkg: Manifest,
beforeFiles: Map<string, number>,
spinner: ReporterSetSpinner,
): Promise<void> {
const afterFiles = await this.walk(loc);
// work out what files have been created/modified
const buildArtifacts = [];
for (const [file, mtime] of afterFiles) {
if (!beforeFiles.has(file) || beforeFiles.get(file) !== mtime) {
buildArtifacts.push(file);
}
}
if (!buildArtifacts.length) {
// nothing else to do here since we have no build artifacts
return;
}
// set build artifacts
const ref = pkg._reference;
invariant(ref, 'expected reference');
this.artifacts[`${pkg.name}@${pkg.version}`] = buildArtifacts;
}
async install(cmds: Array<[string, string]>, pkg: Manifest, spinner: ReporterSetSpinner): Promise<void> {
const ref = pkg._reference;
invariant(ref, 'expected reference');
const locs = ref.locations;
let updateProgress;
if (cmds.length > 0) {
updateProgress = data => {
const dataStr = data
.toString() // turn buffer into string
.trim(); // trim whitespace
invariant(spinner && spinner.tick, 'We should have spinner and its ticker here');
if (dataStr) {
spinner.tick(
dataStr
// Only get the last line
.substr(dataStr.lastIndexOf('\n') + 1)
// change tabs to spaces as they can interfere with the console
.replace(/\t/g, ' '),
);
}
};
}
try {
for (const [stage, cmd] of cmds) {
await Promise.all(
locs.map(async loc => {
const {stdout} = await executeLifecycleScript({
stage,
config: this.config,
cwd: loc,
cmd,
isInteractive: false,
updateProgress,
});
this.reporter.verbose(stdout);
}),
);
}
} catch (err) {
err.message = `${locs.join(', ')}: ${err.message}`;
invariant(ref, 'expected reference');
if (ref.optional) {
ref.ignore = true;
ref.incompatible = true;
this.reporter.warn(this.reporter.lang('optionalModuleScriptFail', err.message));
this.reporter.info(this.reporter.lang('optionalModuleFail'));
// Cleanup node_modules
try {
await Promise.all(
locs.map(async loc => {
await fsUtil.unlink(loc);
}),
);
} catch (e) {
this.reporter.error(this.reporter.lang('optionalModuleCleanupFail', e.message));
}
} else {
throw err;
}
}
}
packageCanBeInstalled(pkg: Manifest): boolean {
const cmds = this.getInstallCommands(pkg);
if (!cmds.length) {
return false;
}
if (this.config.packBuiltPackages && pkg.prebuiltVariants) {
for (const variant in pkg.prebuiltVariants) {
if (pkg._remote && pkg._remote.reference && pkg._remote.reference.includes(variant)) {
return false;
}
}
}
const ref = pkg._reference;
invariant(ref, 'Missing package reference');
if (!ref.fresh && !this.force) {
// this package hasn't been touched
return false;
}
// Don't run lifecycle scripts for hoisted packages
if (!ref.locations.length) {
return false;
}
// we haven't actually written this module out
if (ref.ignore) {
return false;
}
return true;
}
async runCommand(spinner: ReporterSetSpinner, pkg: Manifest): Promise<void> {
const cmds = this.getInstallCommands(pkg);
spinner.setPrefix(++this.installed, pkg.name);
await this.install(cmds, pkg, spinner);
}
// detect if there is a circularDependency in the dependency tree
detectCircularDependencies(root: Manifest, seenManifests: Set<Manifest>, pkg: Manifest): boolean {
const ref = pkg._reference;
invariant(ref, 'expected reference');
const deps = ref.dependencies;
for (const dep of deps) {
const pkgDep = this.resolver.getStrictResolvedPattern(dep);
if (seenManifests.has(pkgDep)) {
// there is a cycle but not with the root
continue;
}
seenManifests.add(pkgDep);
// found a dependency pointing to root
if (pkgDep == root) {
return true;
}
if (this.detectCircularDependencies(root, seenManifests, pkgDep)) {
return true;
}
}
return false;
}
// find the next package to be installed
findInstallablePackage(workQueue: Set<Manifest>, installed: Set<Manifest>): ?Manifest {
for (const pkg of workQueue) {
const ref = pkg._reference;
invariant(ref, 'expected reference');
const deps = ref.dependencies;
let dependenciesFulfilled = true;
for (const dep of deps) {
const pkgDep = this.resolver.getStrictResolvedPattern(dep);
if (!installed.has(pkgDep)) {
dependenciesFulfilled = false;
break;
}
}
// all dependencies are installed
if (dependenciesFulfilled) {
return pkg;
}
// detect circular dependency, mark this pkg as installable to break the circle
if (this.detectCircularDependencies(pkg, new Set(), pkg)) {
return pkg;
}
}
return null;
}
async worker(
spinner: ReporterSetSpinner,
workQueue: Set<Manifest>,
installed: Set<Manifest>,
waitQueue: Set<() => void>,
): Promise<void> {
while (workQueue.size > 0) {
// find a installable package
const pkg = this.findInstallablePackage(workQueue, installed);
// can't find a package to install, register into waitQueue
if (pkg == null) {
spinner.clear();
await new Promise(resolve => waitQueue.add(resolve));
continue;
}
// found a package to install
workQueue.delete(pkg);
if (this.packageCanBeInstalled(pkg)) {
await this.runCommand(spinner, pkg);
}
installed.add(pkg);
for (const workerResolve of waitQueue) {
workerResolve();
}
waitQueue.clear();
}
}
async init(seedPatterns: Array<string>): Promise<void> {
const workQueue = new Set();
const installed = new Set();
const pkgs = this.resolver.getTopologicalManifests(seedPatterns);
let installablePkgs = 0;
// A map to keep track of what files exist before installation
const beforeFilesMap = new Map();
for (const pkg of pkgs) {
if (this.packageCanBeInstalled(pkg)) {
const ref = pkg._reference;
invariant(ref, 'expected reference');
await Promise.all(
ref.locations.map(async loc => {
beforeFilesMap.set(loc, await this.walk(loc));
installablePkgs += 1;
}),
);
}
workQueue.add(pkg);
}
const set = this.reporter.activitySet(installablePkgs, Math.min(installablePkgs, this.config.childConcurrency));
// waitQueue acts like a semaphore to allow workers to register to be notified
// when there are more work added to the work queue
const waitQueue = new Set();
await Promise.all(set.spinners.map(spinner => this.worker(spinner, workQueue, installed, waitQueue)));
// generate built package as prebuilt one for offline mirror
const offlineMirrorPath = this.config.getOfflineMirrorPath();
if (this.config.packBuiltPackages && offlineMirrorPath) {
for (const pkg of pkgs) {
if (this.packageCanBeInstalled(pkg)) {
let prebuiltPath = path.join(offlineMirrorPath, 'prebuilt');
await fsUtil.mkdirp(prebuiltPath);
const prebuiltFilename = getPlatformSpecificPackageFilename(pkg);
prebuiltPath = path.join(prebuiltPath, prebuiltFilename + '.tgz');
const ref = pkg._reference;
invariant(ref, 'expected reference');
const builtPackagePaths = ref.locations;
await Promise.all(
builtPackagePaths.map(async builtPackagePath => {
// don't use pack command, we want to avoid the file filters logic
const stream = await packWithIgnoreAndHeaders(builtPackagePath);
const hash = await new Promise((resolve, reject) => {
const validateStream = new crypto.HashStream();
stream
.pipe(validateStream)
.pipe(fs.createWriteStream(prebuiltPath))
.on('error', reject)
.on('close', () => resolve(validateStream.getHash()));
});
pkg.prebuiltVariants = pkg.prebuiltVariants || {};
pkg.prebuiltVariants[prebuiltFilename] = hash;
}),
);
}
}
} else {
// cache all build artifacts
for (const pkg of pkgs) {
if (this.packageCanBeInstalled(pkg)) {
const ref = pkg._reference;
invariant(ref, 'expected reference');
const beforeFiles = ref.locations.map(loc => beforeFilesMap.get(loc));
await Promise.all(
beforeFiles.map(async (b, index) => {
invariant(b, 'files before installation should always be recorded');
await this.saveBuildArtifacts(ref.locations[index], pkg, b, set.spinners[0]);
}),
);
}
}
}
set.end();
}
}