diff --git a/lib/versioning/composer/index.ts b/lib/versioning/composer/index.ts index ee668e8dfee89afec811a6b6d4b98bac88269c30..4e29b5dfbc318c5790a90d52e343f83d011b78fa 100644 --- a/lib/versioning/composer/index.ts +++ b/lib/versioning/composer/index.ts @@ -90,51 +90,79 @@ function composer2npm(input: string): string { return output + stability; } -const equals = (a: string, b: string): boolean => - npm.equals(composer2npm(a), composer2npm(b)); +function equals(a: string, b: string): boolean { + return npm.equals(composer2npm(a), composer2npm(b)); +} -const getMajor = (version: string): number => - npm.getMajor(semver.coerce(composer2npm(version))); +function getMajor(version: string): number | null { + const semverVersion = semver.coerce(composer2npm(version)); + return semverVersion ? npm.getMajor(semverVersion) : null; +} -const getMinor = (version: string): number => - npm.getMinor(semver.coerce(composer2npm(version))); +function getMinor(version: string): number | null { + const semverVersion = semver.coerce(composer2npm(version)); + return semverVersion ? npm.getMinor(semverVersion) : null; +} -const getPatch = (version: string): number => - npm.getPatch(semver.coerce(composer2npm(version))); +function getPatch(version: string): number | null { + const semverVersion = semver.coerce(composer2npm(version)); + return semverVersion ? npm.getPatch(semverVersion) : null; +} -const isGreaterThan = (a: string, b: string): boolean => - npm.isGreaterThan(composer2npm(a), composer2npm(b)); +function isGreaterThan(a: string, b: string): boolean { + return npm.isGreaterThan(composer2npm(a), composer2npm(b)); +} -const isLessThanRange = (version: string, range: string): boolean => - npm.isLessThanRange(composer2npm(version), composer2npm(range)); +function isLessThanRange(version: string, range: string): boolean { + return !!npm.isLessThanRange?.(composer2npm(version), composer2npm(range)); +} -const isSingleVersion = (input: string): boolean => - !!input && npm.isSingleVersion(composer2npm(input)); +function isSingleVersion(input: string): boolean { + return !!input && npm.isSingleVersion(composer2npm(input)); +} -const isStable = (version: string): boolean => - version && npm.isStable(composer2npm(version)); +function isStable(version: string): boolean { + return !!(version && npm.isStable(composer2npm(version))); +} -export const isValid = (input: string): boolean => - !!input && npm.isValid(composer2npm(input)); +export function isValid(input: string): boolean { + return !!input && npm.isValid(composer2npm(input)); +} -export const isVersion = (input: string): boolean => - !!input && npm.isVersion(composer2npm(input)); +export function isVersion(input: string): boolean { + return !!input && npm.isVersion(composer2npm(input)); +} -const matches = (version: string, range: string): boolean => - npm.matches(composer2npm(version), composer2npm(range)); +function matches(version: string, range: string): boolean { + return npm.matches(composer2npm(version), composer2npm(range)); +} -const getSatisfyingVersion = (versions: string[], range: string): string => - npm.getSatisfyingVersion(versions.map(composer2npm), composer2npm(range)); +function getSatisfyingVersion( + versions: string[], + range: string +): string | null { + return npm.getSatisfyingVersion( + versions.map(composer2npm), + composer2npm(range) + ); +} -const minSatisfyingVersion = (versions: string[], range: string): string => - npm.minSatisfyingVersion(versions.map(composer2npm), composer2npm(range)); +function minSatisfyingVersion( + versions: string[], + range: string +): string | null { + return npm.minSatisfyingVersion( + versions.map(composer2npm), + composer2npm(range) + ); +} function getNewValue({ currentValue, rangeStrategy, currentVersion, newVersion, -}: NewValueConfig): string { +}: NewValueConfig): string | null { if (rangeStrategy === 'pin') { return newVersion; } @@ -149,9 +177,10 @@ function getNewValue({ newVersion, }); } + const currentMajor = currentVersion ? getMajor(currentVersion) : null; const toMajor = getMajor(newVersion); const toMinor = getMinor(newVersion); - let newValue: string; + let newValue: string | null = null; if (isVersion(currentValue)) { newValue = newVersion; } else if (regEx(/^[~^](0\.[1-9][0-9]*)$/).test(currentValue)) { @@ -166,15 +195,19 @@ function getNewValue({ // handle ~4 case const operator = currentValue.substr(0, 1); newValue = `${operator}${toMajor}`; - } else if (regEx(/^[~^]([0-9]*(?:\.[0-9]*)?)$/).test(currentValue)) { + } else if ( + toMajor && + regEx(/^[~^]([0-9]*(?:\.[0-9]*)?)$/).test(currentValue) + ) { const operator = currentValue.substr(0, 1); // handle ~4.1 case - if (currentVersion && toMajor > getMajor(currentVersion)) { + if ((currentMajor && toMajor > currentMajor) || !toMinor) { newValue = `${operator}${toMajor}.0`; } else { newValue = `${operator}${toMajor}.${toMinor}`; } } else if ( + currentVersion && npm.isVersion(padZeroes(normalizeVersion(newVersion))) && npm.isValid(normalizeVersion(currentValue)) && composer2npm(currentValue) === normalizeVersion(currentValue) @@ -192,18 +225,17 @@ function getNewValue({ } else { const hasOr = currentValue.includes(' || '); if (hasOr || rangeStrategy === 'widen') { - const lastValue = hasOr - ? currentValue.split('||').pop().trim() - : currentValue; + const splitValues = currentValue.split('||'); + const lastValue = splitValues[splitValues.length - 1]; const replacementValue = getNewValue({ - currentValue: lastValue, + currentValue: lastValue.trim(), rangeStrategy: 'replace', currentVersion, newVersion, }); if (rangeStrategy === 'replace') { newValue = replacementValue; - } else { + } else if (replacementValue) { const parsedRange = parseRange(replacementValue); const element = parsedRange[parsedRange.length - 1]; if (element.operator?.startsWith('<')) { diff --git a/tsconfig.strict.json b/tsconfig.strict.json index 5a35b006c4955f4c39d93deac43a1df068e0483f..be31c78ea0ca86ced416c5963f86b3992c867308 100644 --- a/tsconfig.strict.json +++ b/tsconfig.strict.json @@ -389,7 +389,6 @@ "lib/versioning/api.ts", "lib/versioning/aws-machine-image/index.ts", "lib/versioning/common.ts", - "lib/versioning/composer/index.ts", "lib/versioning/generic.ts", "lib/versioning/git/index.ts", "lib/versioning/hashicorp/index.ts",