@@ -119,38 +119,40 @@ export function calculateMismatchingVersions(
119119 return [ ] ;
120120 }
121121
122- // Calculate unique versions seen for this dependency.
123- const uniqueVersions = [
124- ...new Set (
125- versionObjectsForDep
126- . filter ( ( versionObject ) => ! versionObject . isLocalPackageVersion )
127- . map ( ( versionObject ) => versionObject . version )
128- ) ,
129- ] . sort ( ) ;
130-
131- // If we saw more than one unique version for this dependency, we found an inconsistency.
132- if ( uniqueVersions . length > 1 ) {
133- const uniqueVersionsWithInfo = versionsObjectsWithSortedPackages (
134- uniqueVersions ,
135- versionObjectsForDep
136- ) ;
137- return { dependency, versions : uniqueVersionsWithInfo } ;
138- }
122+ // Check what versions we have seen for this dependency.
123+ let versions = versionObjectsForDep
124+ . filter ( ( versionObject ) => ! versionObject . isLocalPackageVersion )
125+ . map ( ( versionObject ) => versionObject . version ) ;
139126
140- // If we saw a local package version that isn't compatible with the local package's actual version, we found an inconsistency .
127+ // Check if this dependency is a local package.
141128 const localPackageVersions = versionObjectsForDep
142- . filter ( ( object ) => object . isLocalPackageVersion )
143- . map ( ( object ) => object . version ) ;
129+ . filter ( ( versionObject ) => versionObject . isLocalPackageVersion )
130+ . map ( ( versionObject ) => versionObject . version ) ;
131+
144132 if (
145133 localPackageVersions . length === 1 &&
146- uniqueVersions . length === 1 &&
147- ! semver . satisfies ( localPackageVersions [ 0 ] , uniqueVersions [ 0 ] )
134+ versions . some (
135+ ( uniqueVersion ) =>
136+ ! semver . satisfies ( localPackageVersions [ 0 ] , uniqueVersion )
137+ )
148138 ) {
139+ // If we saw a version for this dependency that isn't compatible with its actual local package version, add the local package version to the list of versions seen.
140+ versions = [ ...versions , ...localPackageVersions ] ;
141+ }
142+
143+ // Calculate unique versions seen for this dependency.
144+ const uniqueVersions = [ ...new Set ( versions ) ] . sort ( compareRangesSafe ) ;
145+
146+ // If we saw more than one unique version for this dependency, we found an inconsistency.
147+ if ( uniqueVersions . length > 1 ) {
149148 const uniqueVersionsWithInfo = versionsObjectsWithSortedPackages (
150- [ ... uniqueVersions , ... localPackageVersions ] ,
149+ uniqueVersions ,
151150 versionObjectsForDep
152151 ) ;
153- return { dependency, versions : uniqueVersionsWithInfo } ;
152+ return {
153+ dependency,
154+ versions : uniqueVersionsWithInfo ,
155+ } ;
154156 }
155157
156158 return [ ] ;
@@ -254,7 +256,9 @@ export function fixMismatchingVersions(
254256} {
255257 const fixed = [ ] ;
256258 const notFixed = [ ] ;
259+ // Loop through each dependency that has a mismatching versions.
257260 for ( const mismatchingVersion of mismatchingVersions ) {
261+ // Decide what version we should fix to.
258262 const versions = mismatchingVersion . versions . map (
259263 ( object ) => object . version
260264 ) ;
@@ -267,6 +271,21 @@ export function fixMismatchingVersions(
267271 continue ;
268272 }
269273
274+ // If this dependency is from a local package and the version we want to fix to is higher than the actual package version, skip it.
275+ const localPackage = packages . find (
276+ ( package_ ) => package_ . name === mismatchingVersion . dependency
277+ ) ;
278+ if (
279+ localPackage &&
280+ localPackage . packageJson . version &&
281+ compareRanges ( fixedVersion , localPackage . packageJson . version ) > 0
282+ ) {
283+ // Skip this dependency.
284+ notFixed . push ( mismatchingVersion ) ;
285+ continue ;
286+ }
287+
288+ // Update the dependency version in each package.json.
270289 let isFixed = false ;
271290 for ( const package_ of packages ) {
272291 if (
@@ -304,8 +323,6 @@ export function fixMismatchingVersions(
304323
305324 if ( isFixed ) {
306325 fixed . push ( mismatchingVersion ) ;
307- } else {
308- notFixed . push ( mismatchingVersion ) ;
309326 }
310327 }
311328
@@ -315,6 +332,15 @@ export function fixMismatchingVersions(
315332 } ;
316333}
317334
335+ // This version doesn't throw for when we want to ignore invalid versions that might be present.
336+ export function compareRangesSafe ( a : string , b : string ) : 0 | - 1 | 1 {
337+ try {
338+ return compareRanges ( a , b ) ;
339+ } catch {
340+ return 0 ;
341+ }
342+ }
343+
318344export function compareRanges ( a : string , b : string ) : 0 | - 1 | 1 {
319345 // Strip range and coerce to normalized version.
320346 const aVersion = semver . coerce ( a . replace ( / ^ [ \^ ~ ] / , '' ) ) ;
0 commit comments