22// See LICENSE in the project root for license information.
33
44import * as colors from 'colors' ;
5- import * as fsx from 'fs-extra' ;
6- import * as os from 'os' ;
75import * as semver from 'semver' ;
86import npmPackageArg = require( 'npm-package-arg' ) ;
97
108import { Utilities , RushConstants } from '@microsoft/rush-lib' ;
119
12- interface IShrinkwrapDependencyJson {
13- version : string ;
14- from : string ;
15- resolved : string ;
16- dependencies : { [ dependency : string ] : IShrinkwrapDependencyJson } ;
17- }
18-
19- interface IShrinkwrapJson {
20- name : string ;
21- version : string ;
22- dependencies : { [ dependency : string ] : IShrinkwrapDependencyJson } ;
23- }
24-
2510/**
26- * This class is a parser for NPM's npm-shrinkwrap.json file format .
11+ * This class is a parser for both NPM's npm-shrinkwrap.json and PNPM's shrinkwrap.yaml file formats .
2712 */
28- export default class ShrinkwrapFile {
29- private _shrinkwrapJson : IShrinkwrapJson ;
30- private _alreadyWarnedSpecs : Set < string > = new Set < string > ( ) ;
31-
32- public static loadFromFile ( shrinkwrapJsonFilename : string ) : ShrinkwrapFile | undefined {
33- let data : string | undefined = undefined ;
34- try {
35- if ( ! fsx . existsSync ( shrinkwrapJsonFilename ) ) {
36- return undefined ; // file does not exist
37- }
13+ export default abstract class ShrinkwrapFile {
14+ protected _alreadyWarnedSpecs : Set < string > = new Set < string > ( ) ;
3815
39- // We don't use JsonFile/jju here because shrinkwrap.json is a special NPM file format
40- // and typically very large, so we want to load it the same way that NPM does.
41- data = fsx . readFileSync ( shrinkwrapJsonFilename ) . toString ( ) ;
42- if ( data . charCodeAt ( 0 ) === 0xFEFF ) { // strip BOM
43- data = data . slice ( 1 ) ;
44- }
45-
46- return new ShrinkwrapFile ( JSON . parse ( data ) ) ;
47- } catch ( error ) {
48- throw new Error ( `Error reading "${ shrinkwrapJsonFilename } ":` + os . EOL + ` ${ error . message } ` ) ;
49- }
16+ public static loadFromFile ( shrinkwrapFilename : string ) : ShrinkwrapFile | undefined {
17+ return require ( './npm/NpmShrinkwrapFile' ) . default . loadFromFile ( shrinkwrapFilename )
18+ || require ( './npm/PnpmShrinkwrapFile' ) . default . loadFromFile ( shrinkwrapFilename ) ;
5019 }
5120
52- private static tryGetValue < T > ( dictionary : { [ key2 : string ] : T } , key : string ) : T | undefined {
21+ protected static tryGetValue < T > ( dictionary : { [ key2 : string ] : T } , key : string ) : T | undefined {
5322 if ( dictionary . hasOwnProperty ( key ) ) {
5423 return dictionary [ key ] ;
5524 }
5625 return undefined ;
5726 }
5827
59- /**
60- * Returns the list of temp projects defined in this file.
61- * Example: [ '@rush-temp/project1', '@rush-temp/project2' ]
62- */
63- public getTempProjectNames ( ) : ReadonlyArray < string > {
64- const result : string [ ] = [ ] ;
65- for ( const key of Object . keys ( this . _shrinkwrapJson . dependencies ) ) {
66- // If it starts with @rush-temp, then include it:
67- if ( Utilities . parseScopedPackageName ( key ) . scope === RushConstants . rushTempNpmScope ) {
68- result . push ( key ) ;
69- }
70- }
71- result . sort ( ) ; // make the result deterministic
72- return result ;
73- }
74-
7528 /**
7629 * Returns true if the shrinkwrap file includes a package that would satisfiying the specified
77- * package name and SemVer version range. By default, the dependencies are resolved by looking
78- * at the root of the node_modules folder described by the shrinkwrap file. However, if
79- * tempProjectName is specified, then the resolution will start in that subfolder.
80- *
81- * Consider this example:
82- *
83- * - node_modules\
84- * - temp-project\
85- * - lib-a@1.2.3
86- * - lib-b@1.0.0
87- * - lib-b@2.0.0
88- *
89- * In this example, hasCompatibleDependency("lib-b", ">= 1.1.0", "temp-project") would fail
90- * because it finds lib-b@1.0.0 which does not satisfy the pattern ">= 1.1.0".
30+ * package name and SemVer version range.
9131 */
9232 public hasCompatibleDependency ( dependencyName : string , versionRange : string , tempProjectName ?: string ) : boolean {
93-
94- // First, check under tempProjectName, as this is the first place "rush link" looks.
95- let dependencyJson : IShrinkwrapDependencyJson | undefined = undefined ;
96-
97- if ( tempProjectName ) {
98- const tempDependency : IShrinkwrapDependencyJson | undefined = ShrinkwrapFile . tryGetValue (
99- this . _shrinkwrapJson . dependencies , tempProjectName ) ;
100- if ( tempDependency && tempDependency . dependencies ) {
101- dependencyJson = ShrinkwrapFile . tryGetValue ( tempDependency . dependencies , dependencyName ) ;
102- }
103- }
104-
105- // Otherwise look at the root of the shrinkwrap file
106- if ( ! dependencyJson ) {
107- dependencyJson = ShrinkwrapFile . tryGetValue ( this . _shrinkwrapJson . dependencies , dependencyName ) ;
108- }
109-
110- if ( ! dependencyJson ) {
33+ const dependencyVersion : string | undefined = this . getDependencyVersion ( dependencyName , tempProjectName ) ;
34+ if ( ! dependencyVersion ) {
11135 return false ;
11236 }
11337
@@ -116,7 +40,7 @@ export default class ShrinkwrapFile {
11640 case 'version' :
11741 case 'range' :
11842 // If it's a SemVer pattern, then require that the shrinkwrapped version must be compatible
119- return semver . satisfies ( dependencyJson . version , versionRange ) ;
43+ return semver . satisfies ( dependencyVersion , versionRange ) ;
12044 default :
12145 // Only warn once for each spec
12246 if ( ! this . _alreadyWarnedSpecs . has ( result . rawSpec ) ) {
@@ -127,18 +51,23 @@ export default class ShrinkwrapFile {
12751 }
12852 }
12953
130- private constructor ( shrinkwrapJson : IShrinkwrapJson ) {
131- this . _shrinkwrapJson = shrinkwrapJson ;
54+ /**
55+ * Returns the list of temp projects defined in this file.
56+ * Example: [ '@rush-temp/project1', '@rush-temp/project2' ]
57+ */
58+ public abstract getTempProjectNames ( ) : ReadonlyArray < string > ;
59+
60+ protected abstract getDependencyVersion ( dependencyName : string , tempProjectName ?: string ) : string | undefined ;
13261
133- // Normalize the data
134- if ( ! this . _shrinkwrapJson . version ) {
135- this . _shrinkwrapJson . version = '' ;
136- }
137- if ( ! this . _shrinkwrapJson . name ) {
138- this . _shrinkwrapJson . name = '' ;
139- }
140- if ( ! this . _shrinkwrapJson . dependencies ) {
141- this . _shrinkwrapJson . dependencies = { } ;
62+ protected _getTempProjectNames ( dependencies : { [ key : string ] : { } } ) : ReadonlyArray < string > {
63+ const result : string [ ] = [ ] ;
64+ for ( const key of Object . keys ( dependencies ) ) {
65+ // If it starts with @rush-temp, then include it:
66+ if ( Utilities . parseScopedPackageName ( key ) . scope === RushConstants . rushTempNpmScope ) {
67+ result . push ( key ) ;
68+ }
14269 }
70+ result . sort ( ) ; // make the result deterministic
71+ return result ;
14372 }
14473}
0 commit comments