-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PnpLinker.ts
292 lines (220 loc) Β· 12 KB
/
PnpLinker.ts
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
import {Installer, Linker, LinkOptions, MinimalLinkOptions, Manifest, LinkType, MessageName, DependencyMeta} from '@berry/core';
import {FetchResult, Descriptor, Ident, Locator, Package, BuildDirective, BuildType} from '@berry/core';
import {miscUtils, structUtils} from '@berry/core';
import {CwdFS, FakeFS, NodeFS, xfs, PortablePath, ppath, toFilename} from '@berry/fslib';
import {PackageRegistry, generateInlinedScript, generateSplitScript} from '@berry/pnp';
// Some packages do weird stuff and MUST be unplugged. I don't like them.
const FORCED_UNPLUG_PACKAGES = new Set([
structUtils.makeIdent(null, `nan`).identHash,
structUtils.makeIdent(null, `node-gyp`).identHash,
structUtils.makeIdent(null, `node-pre-gyp`).identHash,
structUtils.makeIdent(null, `node-addon-api`).identHash,
]);
export class PnpLinker implements Linker {
supportsPackage(pkg: Package, opts: MinimalLinkOptions) {
return true;
}
async findPackageLocation(locator: Locator, opts: LinkOptions) {
const pnpPath = opts.project.configuration.get(`pnpPath`);
if (!xfs.existsSync(pnpPath))
throw new Error(`Couldn't find the PnP package map at the root of the project - run an install to generate it`);
const physicalPath = NodeFS.fromPortablePath(pnpPath);
const pnpFile = miscUtils.dynamicRequire(physicalPath);
delete require.cache[physicalPath];
const packageLocator = {name: structUtils.requirableIdent(locator), reference: locator.reference};
const packageInformation = pnpFile.getPackageInformation(packageLocator);
if (!packageInformation)
throw new Error(`Couldn't find ${structUtils.prettyLocator(opts.project.configuration, locator)} in the currently installed pnp map`);
return NodeFS.toPortablePath(packageInformation.packageLocation);
}
async findPackageLocator(location: PortablePath, opts: LinkOptions) {
const pnpPath = opts.project.configuration.get(`pnpPath`);
if (!xfs.existsSync(pnpPath))
throw new Error(`Couldn't find the PnP package map at the root of the project - run an install to generate it`);
const physicalPath = NodeFS.fromPortablePath(pnpPath);
const pnpFile = miscUtils.dynamicRequire(physicalPath);
delete require.cache[physicalPath];
const locator = pnpFile.findPackageLocator(NodeFS.fromPortablePath(location));
if (!locator)
return null;
return structUtils.makeLocator(structUtils.parseIdent(locator.name), locator.reference);
}
makeInstaller(opts: LinkOptions) {
return new PnpInstaller(opts);
}
}
class PnpInstaller implements Installer {
private readonly packageRegistry: PackageRegistry = new Map();
private readonly unpluggedPaths: Set<string> = new Set();
private readonly opts: LinkOptions;
constructor(opts: LinkOptions) {
this.opts = opts;
}
async installPackage(pkg: Package, fetchResult: FetchResult) {
const key1 = structUtils.requirableIdent(pkg);
const key2 = pkg.reference;
const buildScripts = await this.getBuildScripts(fetchResult);
if (buildScripts.length > 0 && !this.opts.project.configuration.get(`enableScripts`)) {
this.opts.report.reportWarning(MessageName.DISABLED_BUILD_SCRIPTS, `${structUtils.prettyLocator(this.opts.project.configuration, pkg)} lists build scripts, but all build scripts have been disabled.`);
buildScripts.length = 0;
}
if (buildScripts.length > 0 && pkg.linkType !== LinkType.HARD && !this.opts.project.tryWorkspaceByLocator(pkg)) {
this.opts.report.reportWarning(MessageName.SOFT_LINK_BUILD, `${structUtils.prettyLocator(this.opts.project.configuration, pkg)} lists build scripts, but is referenced through a soft link. Soft links don't support build scripts, so they'll be ignored.`);
buildScripts.length = 0;
}
const dependencyMeta = this.opts.project.getDependencyMeta(pkg, pkg.version);
if (buildScripts.length > 0 && dependencyMeta && dependencyMeta.built === false) {
this.opts.report.reportInfo(MessageName.BUILD_DISABLED, `${structUtils.prettyLocator(this.opts.project.configuration, pkg)} lists build scripts, but its build has been explicitly disabled through configuration.`);
buildScripts.length = 0;
}
const packageFs = pkg.linkType !== LinkType.SOFT && (buildScripts.length > 0 || this.isUnplugged(pkg, dependencyMeta))
? await this.unplugPackage(pkg, fetchResult.packageFs)
: fetchResult.packageFs;
const packageRawLocation = ppath.resolve(packageFs.getRealPath(), ppath.relative(PortablePath.root, fetchResult.prefixPath));
const packageLocation = this.normalizeDirectoryPath(packageRawLocation);
const packageDependencies = new Map();
const packageStore = this.getPackageStore(key1);
packageStore.set(key2, {packageLocation, packageDependencies});
return {
packageLocation,
buildDirective: buildScripts.length > 0 ? buildScripts as BuildDirective[] : null,
};
}
async attachInternalDependencies(locator: Locator, dependencies: Array<[Descriptor, Locator]>) {
const packageInformation = this.getPackageInformation(locator);
packageInformation.packageDependencies = new Map(dependencies.map(([descriptor, locator]) => {
const target = !structUtils.areIdentsEqual(descriptor, locator)
? [structUtils.requirableIdent(locator), locator.reference] as [string, string]
: locator.reference;
return [structUtils.requirableIdent(descriptor), target];
}) as Array<[string, string]>);
}
async attachExternalDependents(locator: Locator, dependentPaths: Array<PortablePath>) {
for (const dependentPath of dependentPaths) {
const packageInformation = this.getDiskInformation(dependentPath);
packageInformation.packageDependencies.set(structUtils.requirableIdent(locator), locator.reference);
}
}
async finalizeInstall() {
if (await this.shouldWarnNodeModules())
this.opts.report.reportWarning(MessageName.DANGEROUS_NODE_MODULES, `One or more node_modules have been detected; they risk hiding legitimate problems until your application reaches production.`);
this.packageRegistry.set(null, new Map([
[null, this.getPackageInformation(this.opts.project.topLevelWorkspace.anchoredLocator)],
]));
const pnpFallbackMode = this.opts.project.configuration.get(`pnpFallbackMode`);
const blacklistedLocations = new Set<string>();
const enableTopLevelFallback = pnpFallbackMode !== `none`;
const fallbackExclusionList = [];
const ignorePattern = this.opts.project.configuration.get(`pnpIgnorePattern`);
const packageRegistry = this.packageRegistry;
const shebang = this.opts.project.configuration.get(`pnpShebang`);
if (pnpFallbackMode === `dependencies-only`)
for (const pkg of this.opts.project.storedPackages.values())
if (this.opts.project.tryWorkspaceByLocator(pkg))
fallbackExclusionList.push({name: structUtils.requirableIdent(pkg), reference: pkg.reference});
const pnpPath = this.opts.project.configuration.get(`pnpPath`);
const pnpDataPath = this.opts.project.configuration.get(`pnpDataPath`);
const pnpSettings = {blacklistedLocations, enableTopLevelFallback, fallbackExclusionList, ignorePattern, packageRegistry, shebang};
if (this.opts.project.configuration.get(`pnpEnableInlining`)) {
const loaderFile = generateInlinedScript(pnpSettings);
await xfs.changeFilePromise(pnpPath, loaderFile);
await xfs.chmodPromise(pnpPath, 0o755);
await xfs.removePromise(pnpDataPath);
} else {
const dataLocation = ppath.relative(ppath.dirname(pnpPath), pnpDataPath);
const {dataFile, loaderFile} = generateSplitScript({...pnpSettings, dataLocation});
await xfs.changeFilePromise(pnpPath, loaderFile);
await xfs.chmodPromise(pnpPath, 0o755);
await xfs.changeFilePromise(pnpDataPath, dataFile);
await xfs.chmodPromise(pnpDataPath, 0o644);
}
const pnpUnpluggedFolder = this.opts.project.configuration.get(`pnpUnpluggedFolder`);
if (this.unpluggedPaths.size === 0) {
await xfs.removePromise(pnpUnpluggedFolder);
} else {
for (const entry of await xfs.readdirPromise(pnpUnpluggedFolder)) {
const unpluggedPath = ppath.resolve(pnpUnpluggedFolder, entry);
if (!this.unpluggedPaths.has(unpluggedPath)) {
await xfs.removePromise(unpluggedPath);
}
}
}
}
private getPackageStore(key: string) {
let packageStore = this.packageRegistry.get(key);
if (!packageStore)
this.packageRegistry.set(key, packageStore = new Map());
return packageStore;
}
private getPackageInformation(locator: Locator) {
const key1 = structUtils.requirableIdent(locator);
const key2 = locator.reference;
const packageInformationStore = this.packageRegistry.get(key1);
if (!packageInformationStore)
throw new Error(`Assertion failed: The package information store should have been available (for ${structUtils.prettyIdent(this.opts.project.configuration, locator)})`);
const packageInformation = packageInformationStore.get(key2);
if (!packageInformation)
throw new Error(`Assertion failed: The package information should have been available (for ${structUtils.prettyLocator(this.opts.project.configuration, locator)})`);
return packageInformation;
}
private getDiskInformation(path: PortablePath) {
const packageStore = this.getPackageStore(`@@disk`);
const normalizedPath = this.normalizeDirectoryPath(path);
let diskInformation = packageStore.get(normalizedPath);
if (!diskInformation) {
packageStore.set(normalizedPath, diskInformation = {
packageLocation: normalizedPath,
packageDependencies: new Map(),
});
}
return diskInformation;
}
private async shouldWarnNodeModules() {
for (const workspace of this.opts.project.workspaces) {
const nodeModulesPath = ppath.join(workspace.cwd, toFilename(`node_modules`));
if (!xfs.existsSync(nodeModulesPath))
continue;
const directoryListing = await xfs.readdirPromise(nodeModulesPath);
if (directoryListing.every(entry => entry.startsWith(`.`)))
continue;
return true;
}
return false;
}
private normalizeDirectoryPath(folder: PortablePath) {
let relativeFolder = ppath.relative(this.opts.project.cwd, folder);
if (!relativeFolder.match(/^\.{0,2}\//))
// Don't use ppath.join here, it ignores the `.`
relativeFolder = `./${relativeFolder}` as PortablePath;
return relativeFolder.replace(/\/?$/, '/') as PortablePath;
}
private async getBuildScripts(fetchResult: FetchResult) {
const buildScripts = [];
const {scripts} = await Manifest.find(fetchResult.prefixPath, {baseFs: fetchResult.packageFs});
for (const scriptName of [`preinstall`, `install`, `postinstall`])
if (scripts.has(scriptName))
buildScripts.push([BuildType.SCRIPT, scriptName]);
// Detect cases where a package has a binding.gyp but no install script
const bindingFilePath = ppath.resolve(fetchResult.prefixPath, toFilename(`binding.gyp`));
if (!scripts.has(`install`) && fetchResult.packageFs.existsSync(bindingFilePath))
buildScripts.push([BuildType.SHELLCODE, `node-gyp rebuild`]);
return buildScripts;
}
private getUnpluggedPath(locator: Locator) {
return ppath.resolve(this.opts.project.configuration.get(`pnpUnpluggedFolder`), structUtils.slugifyLocator(locator));
}
private async unplugPackage(locator: Locator, packageFs: FakeFS<PortablePath>) {
const unplugPath = this.getUnpluggedPath(locator);
this.unpluggedPaths.add(unplugPath);
await xfs.mkdirpPromise(unplugPath);
await xfs.copyPromise(unplugPath, PortablePath.dot, {baseFs: packageFs, overwrite: false});
return new CwdFS(unplugPath);
}
private isUnplugged(ident: Ident, dependencyMeta: DependencyMeta) {
if (dependencyMeta.unplugged)
return true;
if (FORCED_UNPLUG_PACKAGES.has(ident.identHash))
return true;
return false;
}
}