-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
package-linker.js
340 lines (290 loc) · 10.1 KB
/
package-linker.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
/* @flow */
import type {Manifest} from './types.js';
import type PackageResolver from './package-resolver.js';
import type {Reporter} from './reporters/index.js';
import type Config from './config.js';
import type {HoistManifestTuples} from './package-hoister.js';
import type {CopyQueueItem} from './util/fs.js';
import PackageHoister from './package-hoister.js';
import * as constants from './constants.js';
import * as promise from './util/promise.js';
import {entries} from './util/misc.js';
import * as fs from './util/fs.js';
import lockMutex from './util/mutex.js';
const invariant = require('invariant');
const cmdShim = promise.promisify(require('cmd-shim'));
const semver = require('semver');
const path = require('path');
type DependencyPairs = Array<{
dep: Manifest,
loc: string
}>;
export async function linkBin(src: string, dest: string): Promise<void> {
if (process.platform === 'win32') {
const unlockMutex = await lockMutex(src);
try {
await cmdShim(src, dest);
} finally {
unlockMutex();
}
} else {
await fs.mkdirp(path.dirname(dest));
await fs.symlink(src, dest);
await fs.chmod(dest, '755');
}
}
export default class PackageLinker {
constructor(config: Config, resolver: PackageResolver) {
this.resolver = resolver;
this.reporter = config.reporter;
this.config = config;
}
reporter: Reporter;
resolver: PackageResolver;
config: Config;
async linkSelfDependencies(pkg: Manifest, pkgLoc: string, targetBinLoc: string): Promise<void> {
targetBinLoc = await fs.realpath(targetBinLoc);
pkgLoc = await fs.realpath(pkgLoc);
for (const [scriptName, scriptCmd] of entries(pkg.bin)) {
const dest = path.join(targetBinLoc, scriptName);
const src = path.join(pkgLoc, scriptCmd);
if (!await fs.exists(src)) {
// TODO maybe throw an error
continue;
}
await linkBin(src, dest);
}
}
async linkBinDependencies(pkg: Manifest, dir: string): Promise<void> {
const deps: DependencyPairs = [];
const ref = pkg._reference;
invariant(ref, 'Package reference is missing');
const remote = pkg._remote;
invariant(remote, 'Package remote is missing');
// link up `bin scripts` in `dependencies`
for (const pattern of ref.dependencies) {
const dep = this.resolver.getStrictResolvedPattern(pattern);
if (dep.bin && Object.keys(dep.bin).length) {
deps.push({dep, loc: this.config.generateHardModulePath(dep._reference)});
}
}
// link up the `bin` scripts in bundled dependencies
if (pkg.bundleDependencies) {
for (const depName of pkg.bundleDependencies) {
const loc = path.join(
this.config.generateHardModulePath(ref),
this.config.getFolder(pkg),
depName,
);
const dep = await this.config.readManifest(loc, remote.registry);
if (dep.bin && Object.keys(dep.bin).length) {
deps.push({dep, loc});
}
}
}
// no deps to link
if (!deps.length) {
return;
}
// ensure our .bin file we're writing these to exists
const binLoc = path.join(dir, '.bin');
await fs.mkdirp(binLoc);
// write the executables
for (const {dep, loc} of deps) {
await this.linkSelfDependencies(dep, loc, binLoc);
}
}
getFlatHoistedTree(patterns: Array<string>): Promise<HoistManifestTuples> {
const hoister = new PackageHoister(this.config, this.resolver);
hoister.seed(patterns);
return Promise.resolve(hoister.init());
}
async copyModules(patterns: Array<string>, linkDuplicates: boolean): Promise<void> {
let flatTree = await this.getFlatHoistedTree(patterns);
// sorted tree makes file creation and copying not to interfere with each other
flatTree = flatTree.sort(function(dep1, dep2): number {
return dep1[0].localeCompare(dep2[0]);
});
// list of artifacts in modules to remove from extraneous removal
const artifactFiles = [];
const copyQueue: Map<string, CopyQueueItem> = new Map();
const hardlinkQueue: Map<string, CopyQueueItem> = new Map();
const hardlinksEnabled = linkDuplicates && await fs.hardlinksWork(this.config.cwd);
const copiedSrcs: Map<string, string> = new Map();
for (const [dest, {pkg, loc: src}] of flatTree) {
const ref = pkg._reference;
invariant(ref, 'expected package reference');
ref.setLocation(dest);
// get a list of build artifacts contained in this module so we can prevent them from being marked as
// extraneous
const metadata = await this.config.readPackageMetadata(src);
for (const file of metadata.artifacts) {
artifactFiles.push(path.join(dest, file));
}
const copiedDest = copiedSrcs.get(src);
if (!copiedDest) {
if (hardlinksEnabled) {
copiedSrcs.set(src, dest);
}
copyQueue.set(dest, {
src,
dest,
onFresh() {
if (ref) {
ref.setFresh(true);
}
},
});
} else {
hardlinkQueue.set(dest, {
src: copiedDest,
dest,
onFresh() {
if (ref) {
ref.setFresh(true);
}
},
});
}
}
// keep track of all scoped paths to remove empty scopes after copy
const scopedPaths = new Set();
// register root & scoped packages as being possibly extraneous
const possibleExtraneous: Set<string> = new Set();
for (const folder of this.config.registryFolders) {
const loc = path.join(this.config.cwd, folder);
if (await fs.exists(loc)) {
const files = await fs.readdir(loc);
let filepath;
for (const file of files) {
filepath = path.join(loc, file);
if (file[0] === '@') { // it's a scope, not a package
scopedPaths.add(filepath);
const subfiles = await fs.readdir(filepath);
for (const subfile of subfiles) {
possibleExtraneous.add(path.join(filepath, subfile));
}
} else {
possibleExtraneous.add(filepath);
}
}
}
}
// linked modules
for (const loc of possibleExtraneous) {
const stat = await fs.lstat(loc);
if (stat.isSymbolicLink()) {
possibleExtraneous.delete(loc);
copyQueue.delete(loc);
}
}
//
let tick;
await fs.copyBulk(Array.from(copyQueue.values()), this.reporter, {
possibleExtraneous,
artifactFiles,
ignoreBasenames: [
constants.METADATA_FILENAME,
constants.TARBALL_FILENAME,
],
onStart: (num: number) => {
tick = this.reporter.progress(num);
},
onProgress(src: string) {
if (tick) {
tick(src);
}
},
});
await fs.hardlinkBulk(Array.from(hardlinkQueue.values()), this.reporter, {
possibleExtraneous,
artifactFiles,
onStart: (num: number) => {
tick = this.reporter.progress(num);
},
onProgress(src: string) {
if (tick) {
tick(src);
}
},
});
// remove all extraneous files that weren't in the tree
for (const loc of possibleExtraneous) {
this.reporter.verbose(
this.reporter.lang('verboseFileRemoveExtraneous', loc),
);
await fs.unlink(loc);
}
// remove any empty scoped directories
for (const scopedPath of scopedPaths) {
const files = await fs.readdir(scopedPath);
if (files.length === 0) {
await fs.unlink(scopedPath);
}
}
//
if (this.config.binLinks) {
const tickBin = this.reporter.progress(flatTree.length);
await promise.queue(flatTree, async ([dest, {pkg}]) => {
const binLoc = path.join(dest, this.config.getFolder(pkg));
await this.linkBinDependencies(pkg, binLoc);
tickBin(dest);
}, 4);
}
}
resolvePeerModules() {
for (const pkg of this.resolver.getManifests()) {
this._resolvePeerModules(pkg);
}
}
_resolvePeerModules(pkg: Manifest) {
const peerDeps = pkg.peerDependencies;
if (!peerDeps) {
return;
}
const ref = pkg._reference;
invariant(ref, 'Package reference is missing');
for (const name in peerDeps) {
const range = peerDeps[name];
const patterns = this.resolver.patternsByPackage[name] || [];
const foundPattern = patterns.find((pattern) => {
const resolvedPattern = this.resolver.getResolvedPattern(pattern);
return resolvedPattern ? this._satisfiesPeerDependency(range, resolvedPattern.version) : false;
});
if (foundPattern) {
ref.addDependencies([foundPattern]);
} else {
const depError = patterns.length > 0 ? 'incorrectPeer' : 'unmetPeer';
const [pkgHuman, depHuman] = [`${pkg.name}@${pkg.version}`, `${name}@${range}`];
this.reporter.warn(this.reporter.lang(depError, pkgHuman, depHuman));
}
}
}
_satisfiesPeerDependency(range: string, version: string): boolean {
return range === '*' || semver.satisfies(version, range, this.config.looseSemver);
}
async init(patterns: Array<string>, linkDuplicates: boolean): Promise<void> {
this.resolvePeerModules();
await this.copyModules(patterns, linkDuplicates);
await this.saveAll(patterns);
}
async save(pattern: string): Promise<void> {
const resolved = this.resolver.getResolvedPattern(pattern);
invariant(resolved, `Couldn't find resolved name/version for ${pattern}`);
const ref = resolved._reference;
invariant(ref, 'Missing reference');
//
const src = this.config.generateHardModulePath(ref);
// link bins
if (this.config.binLinks && resolved.bin && Object.keys(resolved.bin).length && !ref.ignore) {
const folder = this.config.modulesFolder || path.join(this.config.cwd, this.config.getFolder(resolved));
const binLoc = path.join(folder, '.bin');
await fs.mkdirp(binLoc);
await this.linkSelfDependencies(resolved, src, binLoc);
}
}
async saveAll(deps: Array<string>): Promise<void> {
deps = this.resolver.dedupePatterns(deps);
await promise.queue(deps, (dep): Promise<void> => this.save(dep));
}
}