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' ) ;
64import { JsonFile } from '@microsoft/node-core-library' ;
75
86import { 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+ }
0 commit comments