check-dependency-version-consistency
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -37,4 +37,7 @@ import { Package } from './package.js'; | ||
}; | ||
export declare function compareRangesSafe(a: string, b: string): 0 | -1 | 1; | ||
export declare function compareRanges(a: string, b: string): 0 | -1 | 1; | ||
export declare function compareVersionRangesSafe(a: string, b: string): 0 | -1 | 1; | ||
export declare function compareVersionRanges(a: string, b: string): 0 | -1 | 1; | ||
export declare function compareRanges(aRange: string, bRange: string): 0 | -1 | 1; | ||
export declare function versionRangeToRange(versionRange: string): string; | ||
export declare function getLatestVersion(versions: string[]): string; | ||
export declare function getHighestRangeType(ranges: string[]): string; |
@@ -77,3 +77,3 @@ import semver from 'semver'; | ||
// Calculate unique versions seen for this dependency. | ||
const uniqueVersions = [...new Set(versions)].sort(compareRangesSafe); | ||
const uniqueVersions = [...new Set(versions)].sort(compareVersionRangesSafe); | ||
// If we saw more than one unique version for this dependency, we found an inconsistency. | ||
@@ -148,3 +148,3 @@ if (uniqueVersions.length > 1) { | ||
localPackage.packageJson.version && | ||
compareRanges(fixedVersion, localPackage.packageJson.version) > 0) { | ||
compareVersionRanges(fixedVersion, localPackage.packageJson.version) > 0) { | ||
// Skip this dependency. | ||
@@ -154,2 +154,7 @@ notFixed.push(mismatchingVersion); | ||
} | ||
if (localPackage && localPackage.packageJson.version === fixedVersion) { | ||
// When fixing to the version of a local package, don't just use the bare package version, but include the highest range type we have seen. | ||
const highestRangeTypeSeen = getHighestRangeType(versions.map((versionRange) => versionRangeToRange(versionRange))); | ||
fixedVersion = `${highestRangeTypeSeen}${semver.coerce(fixedVersion)}`; | ||
} | ||
// Update the dependency version in each package.json. | ||
@@ -183,5 +188,5 @@ let isFixed = false; | ||
// This version doesn't throw for when we want to ignore invalid versions that might be present. | ||
export function compareRangesSafe(a, b) { | ||
export function compareVersionRangesSafe(a, b) { | ||
try { | ||
return compareRanges(a, b); | ||
return compareVersionRanges(a, b); | ||
} | ||
@@ -192,6 +197,7 @@ catch { | ||
} | ||
export function compareRanges(a, b) { | ||
// Strip range and coerce to normalized version. | ||
const aVersion = semver.coerce(a.replace(/^[\^~]/, '')); | ||
const bVersion = semver.coerce(b.replace(/^[\^~]/, '')); | ||
// Compare semver version ranges like ^1.0.0, ~2.5.0, 3.0.0, etc. | ||
export function compareVersionRanges(a, b) { | ||
// Coerce to normalized version without any range prefix. | ||
const aVersion = semver.coerce(a); | ||
const bVersion = semver.coerce(b); | ||
if (!aVersion) { | ||
@@ -204,17 +210,6 @@ throw new Error(`Invalid Version: ${a}`); | ||
if (semver.eq(aVersion, bVersion)) { | ||
// Same version, but wider range considered higher. | ||
if (a.startsWith('^') && !b.startsWith('^')) { | ||
return 1; | ||
} | ||
else if (!a.startsWith('^') && b.startsWith('^')) { | ||
return -1; | ||
} | ||
else if (a.startsWith('~') && !b.startsWith('~')) { | ||
return 1; | ||
} | ||
else if (!a.startsWith('~') && b.startsWith('~')) { | ||
return -1; | ||
} | ||
// Same version, same range. | ||
return 0; | ||
// Same version, so decide which range is considered higher. | ||
const aRange = versionRangeToRange(a); | ||
const bRange = versionRangeToRange(b); | ||
return compareRanges(aRange, bRange); | ||
} | ||
@@ -224,5 +219,28 @@ // Greater version considered higher. | ||
} | ||
const RANGE_PRECEDENCE = ['~', '^']; // Lowest to highest. | ||
// Compare semver ranges like ^, ~, etc. | ||
export function compareRanges(aRange, bRange) { | ||
const aRangePrecedence = RANGE_PRECEDENCE.indexOf(aRange); | ||
const bRangePrecedence = RANGE_PRECEDENCE.indexOf(bRange); | ||
if (aRangePrecedence > bRangePrecedence) { | ||
return 1; | ||
} | ||
else if (aRangePrecedence < bRangePrecedence) { | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
// Example input: ^1.0.0, output: ^ | ||
export function versionRangeToRange(versionRange) { | ||
const match = versionRange.match(/^\D+/); | ||
return match ? match[0] : ''; | ||
} | ||
export function getLatestVersion(versions) { | ||
const sortedVersions = versions.sort(compareRanges); | ||
const sortedVersions = versions.sort(compareVersionRanges); | ||
return sortedVersions[sortedVersions.length - 1]; // Latest version will be sorted to end of list. | ||
} | ||
// Example input: ['~', '^'], output: '^' | ||
export function getHighestRangeType(ranges) { | ||
const sorted = ranges.sort(compareRanges); | ||
return sorted[sorted.length - 1]; // Range with highest precedence will be sorted to end of list. | ||
} |
import chalk from 'chalk'; | ||
import { compareRangesSafe, getLatestVersion } from './dependency-versions.js'; | ||
import { compareVersionRangesSafe, getLatestVersion, } from './dependency-versions.js'; | ||
import { table } from 'table'; | ||
@@ -15,3 +15,3 @@ export function mismatchingVersionsToOutput(mismatchingDependencyVersions) { | ||
const rows = object.versions | ||
.sort((a, b) => compareRangesSafe(b.version, a.version)) | ||
.sort((a, b) => compareVersionRangesSafe(b.version, a.version)) | ||
.map((versionObject) => { | ||
@@ -18,0 +18,0 @@ const usageCount = versionObject.packages.length; |
{ | ||
"name": "check-dependency-version-consistency", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Ensures dependencies are on consistent versions across a monorepo.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34915
542