22// See LICENSE in the project root for license information.
33
44import * as path from 'path' ;
5+ import * as semver from 'semver' ;
56import {
67 JsonFile ,
78 InternalError ,
@@ -10,11 +11,13 @@ import {
1011
1112import {
1213 PnpmShrinkwrapFile ,
13- IPnpmShrinkwrapDependencyYaml
14+ IPnpmShrinkwrapDependencyYaml ,
15+ parsePnpmDependencyKey
1416} from './PnpmShrinkwrapFile' ;
1517import { RushConfigurationProject } from '../../api/RushConfigurationProject' ;
1618import { RushConstants } from '../RushConstants' ;
1719import { BasePackage } from '../base/BasePackage' ;
20+ import { DependencySpecifier } from '../DependencySpecifier' ;
1821
1922export interface IPnpmProjectDependencyManifestOptions {
2023 pnpmShrinkwrapFile : PnpmShrinkwrapFile ;
@@ -54,12 +57,12 @@ export class PnpmProjectDependencyManifest {
5457 this . _projectDependencyManifestFile = new Map < string , string > ( ) ;
5558 }
5659
57- public addDependency ( pkg : BasePackage ) : void {
60+ public addDependency ( pkg : BasePackage , parentShrinkwrapEntry : IPnpmShrinkwrapDependencyYaml ) : void {
5861 if ( ! pkg . version ) {
5962 throw new InternalError ( `Version missing from dependency ${ pkg . name } ` ) ;
6063 }
6164
62- this . _addDependencyInternal ( pkg . name , pkg . version ) ;
65+ this . _addDependencyInternal ( pkg . name , pkg . version , parentShrinkwrapEntry ) ;
6366 }
6467
6568 /**
@@ -88,7 +91,9 @@ export class PnpmProjectDependencyManifest {
8891
8992 private _addDependencyInternal (
9093 name : string ,
91- version : string
94+ version : string ,
95+ parentShrinkwrapEntry : IPnpmShrinkwrapDependencyYaml ,
96+ throwIfShrinkwrapEntryMissing : boolean = true
9297 ) : void {
9398 const specifier : string = `${ name } @${ version } ` ;
9499 if ( ! PnpmProjectDependencyManifest . _cache . has ( specifier ) ) {
@@ -98,15 +103,92 @@ export class PnpmProjectDependencyManifest {
98103 ) ;
99104
100105 if ( ! shrinkwrapEntry ) {
101- throw new InternalError ( `Unable to find dependency ${ name } with version ${ version } in shrinkwrap.` ) ;
106+ if ( throwIfShrinkwrapEntryMissing ) {
107+ throw new InternalError ( `Unable to find dependency ${ name } with version ${ version } in shrinkwrap.` ) ;
108+ }
109+ return ;
102110 }
103111
104112 PnpmProjectDependencyManifest . _cache . set ( specifier , shrinkwrapEntry . resolution . integrity ) ;
105113
106114 for ( const dependencyName in shrinkwrapEntry . dependencies ) {
107115 if ( shrinkwrapEntry . dependencies . hasOwnProperty ( dependencyName ) ) {
108116 const dependencyVersion : string = shrinkwrapEntry . dependencies [ dependencyName ] ;
109- this . _addDependencyInternal ( dependencyName , dependencyVersion ) ;
117+ this . _addDependencyInternal ( dependencyName , dependencyVersion , shrinkwrapEntry ) ;
118+ }
119+ }
120+
121+ for ( const optionalDependencyName in shrinkwrapEntry . optionalDependencies ) {
122+ if ( shrinkwrapEntry . optionalDependencies . hasOwnProperty ( optionalDependencyName ) ) {
123+ // Optional dependencies may not exist. Don't blow up if it can't be found
124+ const dependencyVersion : string = shrinkwrapEntry . optionalDependencies [ optionalDependencyName ] ;
125+ this . _addDependencyInternal (
126+ optionalDependencyName ,
127+ dependencyVersion ,
128+ shrinkwrapEntry ,
129+ throwIfShrinkwrapEntryMissing = false ) ;
130+ }
131+ }
132+
133+ for ( const peerDependencyName in shrinkwrapEntry . peerDependencies ) {
134+ if ( shrinkwrapEntry . peerDependencies . hasOwnProperty ( peerDependencyName ) ) {
135+ // Peer dependencies come in the form of a semantic version range
136+ const dependencySemVer : string = shrinkwrapEntry . peerDependencies [ peerDependencyName ] ;
137+ // Check the current package to see if the dependency is already satisfied
138+ if (
139+ shrinkwrapEntry . dependencies &&
140+ shrinkwrapEntry . dependencies . hasOwnProperty ( peerDependencyName )
141+ ) {
142+ const dependencySpecifier : DependencySpecifier | undefined = parsePnpmDependencyKey (
143+ peerDependencyName ,
144+ shrinkwrapEntry . dependencies [ peerDependencyName ]
145+ ) ;
146+ if (
147+ dependencySpecifier &&
148+ semver . valid ( dependencySpecifier . versionSpecifier ) &&
149+ semver . satisfies ( dependencySpecifier . versionSpecifier , dependencySemVer )
150+ ) {
151+ continue ;
152+ }
153+ }
154+
155+ // If not, check the parent.
156+ if (
157+ parentShrinkwrapEntry . dependencies &&
158+ parentShrinkwrapEntry . dependencies . hasOwnProperty ( peerDependencyName )
159+ ) {
160+ const dependencySpecifier : DependencySpecifier | undefined = parsePnpmDependencyKey (
161+ peerDependencyName ,
162+ parentShrinkwrapEntry . dependencies [ peerDependencyName ]
163+ ) ;
164+ if (
165+ dependencySpecifier &&
166+ semver . valid ( dependencySpecifier . versionSpecifier ) &&
167+ semver . satisfies ( dependencySpecifier . versionSpecifier , dependencySemVer )
168+ ) {
169+ continue ;
170+ }
171+ }
172+
173+ // The parent doesn't have a version that satisfies the range. As a last attempt, check
174+ // if it's been hoisted up as a top-level dependency
175+ const topLevelDependencySpecifier : DependencySpecifier | undefined =
176+ this . _pnpmShrinkwrapFile . getTopLevelDependencyVersion ( peerDependencyName ) ;
177+ if (
178+ ! topLevelDependencySpecifier ||
179+ ! semver . valid ( topLevelDependencySpecifier . versionSpecifier ) ||
180+ ! semver . satisfies ( topLevelDependencySpecifier . versionSpecifier , dependencySemVer )
181+ ) {
182+ throw new InternalError (
183+ `Could not find peer dependency '${ peerDependencyName } ' that satisfies version '${ dependencySemVer } '`
184+ ) ;
185+ }
186+
187+ this . _addDependencyInternal (
188+ peerDependencyName ,
189+ this . _pnpmShrinkwrapFile . getTopLevelDependencyKey ( peerDependencyName ) ! ,
190+ shrinkwrapEntry
191+ ) ;
110192 }
111193 }
112194 }
0 commit comments