Skip to content

Commit 56edb8a

Browse files
author
Nick Pape
committed
Get the PNPM Link Manager code building
1 parent 67625c1 commit 56edb8a

4 files changed

Lines changed: 475 additions & 185 deletions

File tree

Lines changed: 18 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as path from 'path';
5-
import readPackageTree = require('read-package-tree');
64
import { JsonFile } from '@microsoft/node-core-library';
75

86
import { IPackageJson } from '../../index';
@@ -65,16 +63,6 @@ export default class Package {
6563
*/
6664
public version: string;
6765

68-
/**
69-
* Names of packages that we explicitly depend on. The actual dependency
70-
* package may be found in this.children, or possibly in this.children of
71-
* one of the parents.
72-
* If a dependency is listed in the "optionalDependencies" section of package.json
73-
* then its name here will be prepended with a "?" character, which means that Rush
74-
* will not report an error if the module cannot be found in the Common folder.
75-
*/
76-
public dependencies: IPackageDependency[];
77-
7866
/**
7967
* The absolute path to the folder that contains package.json.
8068
*/
@@ -85,6 +73,8 @@ export default class Package {
8573
*/
8674
public parent: Package | undefined;
8775

76+
public packageJson: IRushTempPackageJson | undefined;
77+
8878
/**
8979
* If this is a local path that we are planning to symlink to a target folder,
9080
* then symlinkTargetFolderPath keeps track of the intended target.
@@ -98,82 +88,14 @@ export default class Package {
9888
public children: Package[];
9989
private _childrenByName: Map<string, Package>;
10090

101-
/**
102-
* Recursive constructs a tree of Package objects using information returned
103-
* by the "read-package-tree" library.
104-
*/
105-
public static createFromNpm(npmPackage: readPackageTree.PackageNode): Package {
106-
if (npmPackage.error) {
107-
throw Error(`Failed to parse package.json for ${path.basename(npmPackage.path)}: `
108-
+ npmPackage.error.message);
109-
}
110-
111-
let dependencies: IPackageDependency[] = [];
112-
const dependencyNames: Set<string> = new Set<string>();
113-
const packageJson: IRushTempPackageJson = npmPackage.package;
114-
115-
if (packageJson.optionalDependencies) {
116-
for (const dependencyName of Object.keys(packageJson.optionalDependencies)) {
117-
if (!dependencyNames.has(dependencyName)) {
118-
dependencyNames.add(dependencyName);
119-
dependencies.push({
120-
name: dependencyName,
121-
versionRange: packageJson.optionalDependencies[dependencyName],
122-
kind: PackageDependencyKind.Optional
123-
});
124-
}
125-
}
126-
}
127-
if (packageJson.dependencies) {
128-
for (const dependencyName of Object.keys(packageJson.dependencies)) {
129-
if (!dependencyNames.has(dependencyName)) {
130-
dependencyNames.add(dependencyName);
131-
dependencies.push({
132-
name: dependencyName,
133-
versionRange: packageJson.dependencies[dependencyName],
134-
kind: PackageDependencyKind.Normal
135-
});
136-
}
137-
}
138-
}
139-
if (packageJson.rushDependencies) {
140-
for (const dependencyName of Object.keys(packageJson.rushDependencies)) {
141-
if (!dependencyNames.has(dependencyName)) {
142-
dependencyNames.add(dependencyName);
143-
dependencies.push({
144-
name: dependencyName,
145-
versionRange: packageJson.dependencies![dependencyName],
146-
kind: PackageDependencyKind.LocalLink
147-
});
148-
}
149-
}
150-
}
151-
152-
dependencies = dependencies.sort((a, b) => a.name.localeCompare(b.name));
153-
154-
const newPackage: Package = new Package(
155-
npmPackage.package.name,
156-
npmPackage.package.version,
157-
dependencies,
158-
// NOTE: We don't use packageNode.realpath here, because if "npm unlink" was
159-
// performed without redoing "rush link", then a broken symlink is better than
160-
// a symlink to the wrong thing.
161-
npmPackage.path
162-
);
163-
164-
for (const child of npmPackage.children) {
165-
newPackage.addChild(Package.createFromNpm(child));
166-
}
167-
168-
return newPackage;
169-
}
170-
17191
/**
17292
* Used by "npm link" when creating a Package object that represents symbolic links to be created.
17393
*/
174-
public static createLinkedPackage(name: string, version: string, dependencies: IPackageDependency[],
175-
folderPath: string): Package {
176-
return new Package(name, version, dependencies, folderPath);
94+
public static createLinkedPackage(name: string,
95+
version: string,
96+
folderPath: string,
97+
packageJson?: IRushTempPackageJson): Package {
98+
return new Package(name, version, folderPath, packageJson);
17799
}
178100

179101
/**
@@ -185,18 +107,8 @@ export default class Package {
185107
* Example: c:\MyRepo\common\temp\node_modules\@rush-temp\project1
186108
*/
187109
public static createVirtualTempPackage(packageJsonFilename: string, installFolderName: string): Package {
188-
const packageJson: IPackageJson = JsonFile.load(packageJsonFilename);
189-
const npmPackage: readPackageTree.PackageNode = {
190-
children: [],
191-
error: undefined,
192-
id: 0,
193-
isLink: false,
194-
package: packageJson,
195-
parent: undefined,
196-
path: installFolderName,
197-
realpath: installFolderName
198-
};
199-
return Package.createFromNpm(npmPackage);
110+
const packageJson: IRushTempPackageJson = JsonFile.load(packageJsonFilename);
111+
return Package.createLinkedPackage(packageJson.name, packageJson.version, installFolderName, packageJson);
200112
}
201113

202114
public get nameAndVersion(): string {
@@ -216,7 +128,7 @@ export default class Package {
216128
return result;
217129
}
218130

219-
public addChild(child: Package): void {
131+
public addChild<T extends Package>(child: T): void {
220132
if (child.parent) {
221133
throw Error('Child already has a parent');
222134
}
@@ -232,59 +144,6 @@ export default class Package {
232144
return this._childrenByName.get(childPackageName);
233145
}
234146

235-
/**
236-
* Searches the node_modules hierarchy for the nearest matching package with the
237-
* given name. Note that the nearest match may have an incompatible version.
238-
* If a match is found, then the "found" result will not be undefined.
239-
* In either case, the parentForCreate result indicates where the missing
240-
* dependency can be added, i.e. if the requested dependency was not found
241-
* or was found with an incompatible version.
242-
*
243-
* "cyclicSubtreeRoot" is a special optional parameter that specifies a different
244-
* root for the tree; the cyclicDependencyProjects feature uses this to isolate
245-
* certain devDependencies in their own subtree.
246-
*/
247-
public resolveOrCreate(dependencyName: string, cyclicSubtreeRoot?: Package): IResolveOrCreateResult {
248-
249-
let currentParent: Package = this;
250-
let parentForCreate: Package | undefined = undefined;
251-
252-
// tslint:disable-next-line:no-constant-condition
253-
while (true) {
254-
// Does any child match?
255-
for (const child of currentParent.children) {
256-
if (child.name === dependencyName) {
257-
// One of the children matched. Note that parentForCreate may be
258-
// undefined, e.g. if an immediate child is found but has the wrong version,
259-
// then we have no place in the tree to create another version.
260-
return { found: child, parentForCreate };
261-
}
262-
}
263-
264-
// If no child matched, then make this node the "parentForCreate" where we
265-
// could add a missing dependency.
266-
parentForCreate = currentParent;
267-
268-
if (!currentParent.parent
269-
|| (cyclicSubtreeRoot && currentParent === cyclicSubtreeRoot)) {
270-
// We reached the root without finding a match
271-
// parentForCreate will be the root.
272-
return { found: undefined, parentForCreate };
273-
}
274-
275-
// Continue walking upwards.
276-
currentParent = currentParent.parent;
277-
}
278-
}
279-
280-
/**
281-
* Searches the node_modules hierarchy for the nearest matching package with the
282-
* given name. If no match is found, then undefined is returned.
283-
*/
284-
public resolve(dependencyName: string): Package | undefined {
285-
return this.resolveOrCreate(dependencyName).found;
286-
}
287-
288147
public printTree(indent?: string): void {
289148
if (!indent) {
290149
indent = '';
@@ -295,21 +154,17 @@ export default class Package {
295154
}
296155
}
297156

298-
private constructor(name: string, version: string, dependencies: IPackageDependency[], folderPath: string) {
157+
protected constructor(name: string,
158+
version: string,
159+
folderPath: string,
160+
packageJson: IRushTempPackageJson | undefined) {
161+
299162
this.name = name;
163+
this.packageJson = packageJson;
300164
this.version = version;
301-
this.dependencies = dependencies.slice(0); // clone the array
302165
this.folderPath = folderPath;
303-
this.parent = undefined;
166+
304167
this.children = [];
305168
this._childrenByName = new Map<string, Package>();
306169
}
307-
}
308-
309-
/**
310-
* Used by the "rush link" algorithm when doing NPM package resolution.
311-
*/
312-
export interface IResolveOrCreateResult {
313-
found: Package | undefined;
314-
parentForCreate: Package | undefined;
315-
}
170+
}

‎apps/rush-lib/src/cli/utilities/npm/NpmLinkManager.ts‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,34 @@ import * as os from 'os';
77
import * as path from 'path';
88
import * as semver from 'semver';
99
import * as tar from 'tar';
10-
1110
import readPackageTree = require('read-package-tree');
11+
1212
import { JsonFile } from '@microsoft/node-core-library';
1313

1414
import { RushConstants } from '../../../RushConstants';
1515
import { IRushLinkJson } from '../../../data/RushConfiguration';
1616
import RushConfigurationProject from '../../../data/RushConfigurationProject';
1717
import Utilities from '../../../utilities/Utilities';
18-
import Package, {
18+
import {
19+
NpmPackage,
1920
IResolveOrCreateResult,
2021
PackageDependencyKind
21-
} from '../Package';
22+
} from './NpmPackage';
2223
import PackageLookup from '../PackageLookup';
2324
import LinkManager, {
2425
SymlinkKind
2526
} from '../LinkManager';
2627

2728
interface IQueueItem {
2829
// A project from somewhere under "common/temp/node_modules"
29-
commonPackage: Package;
30+
commonPackage: NpmPackage;
3031

3132
// A symlinked virtual package that we will create somewhere under "this-project/node_modules"
32-
localPackage: Package;
33+
localPackage: NpmPackage;
3334

3435
// If we encounter a dependency listed in cyclicDependencyProjects, this will be set to the root
3536
// of the localPackage subtree where we will stop creating local links.
36-
cyclicSubtreeRoot: Package | undefined;
37+
cyclicSubtreeRoot: NpmPackage | undefined;
3738
}
3839

3940
export default class NpmLinkManager extends LinkManager {
@@ -45,7 +46,7 @@ export default class NpmLinkManager extends LinkManager {
4546
reject(error);
4647
} else {
4748
try {
48-
const commonRootPackage: Package = Package.createFromNpm(npmPackage);
49+
const commonRootPackage: NpmPackage = NpmPackage.createFromNpm(npmPackage);
4950

5051
const commonPackageLookup: PackageLookup = new PackageLookup();
5152
commonPackageLookup.loadTree(commonRootPackage);
@@ -80,11 +81,12 @@ export default class NpmLinkManager extends LinkManager {
8081
*/
8182
private _linkProject(
8283
project: RushConfigurationProject,
83-
commonRootPackage: Package,
84+
commonRootPackage: NpmPackage,
8485
commonPackageLookup: PackageLookup,
8586
rushLinkJson: IRushLinkJson): void {
8687

87-
let commonProjectPackage: Package | undefined = commonRootPackage.getChildByName(project.tempProjectName);
88+
let commonProjectPackage: NpmPackage | undefined =
89+
commonRootPackage.getChildByName(project.tempProjectName) as NpmPackage;
8890
if (!commonProjectPackage) {
8991
// Normally we would expect the temp project to have been installed into the common\node_modules
9092
// folder. However, if it was recently added, "rush install" doesn't technically require
@@ -116,7 +118,7 @@ export default class NpmLinkManager extends LinkManager {
116118
const installFolderName: string = path.join(this._rushConfiguration.commonTempFolder,
117119
RushConstants.nodeModulesFolderName, RushConstants.rushTempNpmScope, unscopedTempProjectName);
118120

119-
commonProjectPackage = Package.createVirtualTempPackage(packageJsonFilename, installFolderName);
121+
commonProjectPackage = NpmPackage.createVirtualTempPackage(packageJsonFilename, installFolderName);
120122

121123
// remove the extracted tarball contents
122124
fsx.removeSync(packageJsonFilename);
@@ -126,7 +128,7 @@ export default class NpmLinkManager extends LinkManager {
126128
}
127129

128130
// TODO: Validate that the project's package.json still matches the common folder
129-
const localProjectPackage: Package = Package.createLinkedPackage(
131+
const localProjectPackage: NpmPackage = NpmPackage.createLinkedNpmPackage(
130132
project.packageJson.name,
131133
commonProjectPackage.version,
132134
commonProjectPackage.dependencies,
@@ -148,15 +150,15 @@ export default class NpmLinkManager extends LinkManager {
148150
}
149151

150152
// A project from somewhere under "common/temp/node_modules"
151-
const commonPackage: Package = queueItem.commonPackage;
153+
const commonPackage: NpmPackage = queueItem.commonPackage;
152154

153155
// A symlinked virtual package somewhere under "this-project/node_modules",
154156
// where "this-project" corresponds to the "project" parameter for linkProject().
155-
const localPackage: Package = queueItem.localPackage;
157+
const localPackage: NpmPackage = queueItem.localPackage;
156158

157159
// If we encounter a dependency listed in cyclicDependencyProjects, this will be set to the root
158160
// of the localPackage subtree where we will stop creating local links.
159-
const cyclicSubtreeRoot: Package | undefined = queueItem.cyclicSubtreeRoot;
161+
const cyclicSubtreeRoot: NpmPackage | undefined = queueItem.cyclicSubtreeRoot;
160162

161163
// NOTE: It's important that this traversal follows the dependencies in the Common folder,
162164
// because for Rush projects this will be the union of
@@ -215,7 +217,7 @@ export default class NpmLinkManager extends LinkManager {
215217
const newLocalFolderPath: string = path.join(
216218
resolution.parentForCreate!.folderPath, 'node_modules', dependency.name);
217219

218-
const newLocalPackage: Package = Package.createLinkedPackage(
220+
const newLocalPackage: NpmPackage = NpmPackage.createLinkedNpmPackage(
219221
dependency.name,
220222
matchedVersion,
221223
// Since matchingRushProject does not have a parent, its dependencies are
@@ -237,7 +239,7 @@ export default class NpmLinkManager extends LinkManager {
237239

238240
// We can't symlink to an Rush project, so instead we will symlink to a folder
239241
// under the "Common" folder
240-
const commonDependencyPackage: Package | undefined = commonPackage.resolve(dependency.name);
242+
const commonDependencyPackage: NpmPackage | undefined = commonPackage.resolve(dependency.name);
241243
if (commonDependencyPackage) {
242244
// This is the version that was chosen when "npm install" ran in the common folder
243245
const effectiveDependencyVersion: string = commonDependencyPackage.version;
@@ -261,22 +263,22 @@ export default class NpmLinkManager extends LinkManager {
261263
const newLocalFolderPath: string = path.join(
262264
resolution.parentForCreate!.folderPath, 'node_modules', commonDependencyPackage.name);
263265

264-
const newLocalPackage: Package = Package.createLinkedPackage(
266+
const newLocalPackage: NpmPackage = NpmPackage.createLinkedNpmPackage(
265267
commonDependencyPackage.name,
266268
commonDependencyPackage.version,
267269
commonDependencyPackage.dependencies,
268270
newLocalFolderPath
269271
);
270272

271-
const commonPackageFromLookup: Package | undefined =
272-
commonPackageLookup.getPackage(newLocalPackage.nameAndVersion);
273+
const commonPackageFromLookup: NpmPackage | undefined =
274+
commonPackageLookup.getPackage(newLocalPackage.nameAndVersion) as NpmPackage;
273275
if (!commonPackageFromLookup) {
274276
throw Error(`The ${localPackage.name}@${localPackage.version} package was not found`
275277
+ ` in the common folder`);
276278
}
277279
newLocalPackage.symlinkTargetFolderPath = commonPackageFromLookup.folderPath;
278280

279-
let newCyclicSubtreeRoot: Package | undefined = cyclicSubtreeRoot;
281+
let newCyclicSubtreeRoot: NpmPackage | undefined = cyclicSubtreeRoot;
280282
if (startingCyclicSubtree) {
281283
// If we are starting a new subtree, then newLocalPackage will be its root
282284
// NOTE: cyclicSubtreeRoot is guaranteed to be undefined here, since we never start

0 commit comments

Comments
 (0)