Skip to content

Commit

Permalink
refactor(npm): Avoid usage of non-null assertion for constraints (#22405
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zharinov committed May 24, 2023
1 parent 71ce657 commit c146878
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/modules/manager/npm/extract/locked-versions.ts
Expand Up @@ -24,17 +24,22 @@ export async function getLockedVersions(
lockFileCache[yarnLock] = await getYarnLock(yarnLock);
}
const { lockfileVersion, isYarn1 } = lockFileCache[yarnLock];
let yarn: string | undefined;
if (!isYarn1 && !packageFile.extractedConstraints?.yarn) {
if (lockfileVersion && lockfileVersion >= 8) {
// https://github.com/yarnpkg/berry/commit/9bcd27ae34aee77a567dd104947407532fa179b3
packageFile.extractedConstraints!.yarn = '^3.0.0';
yarn = '^3.0.0';
} else if (lockfileVersion && lockfileVersion >= 6) {
// https://github.com/yarnpkg/berry/commit/f753790380cbda5b55d028ea84b199445129f9ba
packageFile.extractedConstraints!.yarn = '^2.2.0';
yarn = '^2.2.0';
} else {
packageFile.extractedConstraints!.yarn = '^2.0.0';
yarn = '^2.0.0';
}
}
if (yarn) {
packageFile.extractedConstraints ??= {};
packageFile.extractedConstraints.yarn = yarn;
}
for (const dep of packageFile.deps) {
dep.lockedVersion =
lockFileCache[yarnLock].lockedVersions[
Expand All @@ -57,30 +62,37 @@ export async function getLockedVersions(
logger.trace('Retrieving/parsing ' + npmLock);
lockFileCache[npmLock] = await getNpmLock(npmLock);
}

const { lockfileVersion } = lockFileCache[npmLock];
let npm: string | undefined;
if (lockfileVersion === 1) {
if (packageFile.extractedConstraints?.npm) {
// Add a <7 constraint if it's not already a fixed version
if (
semver.satisfies('6.14.18', packageFile.extractedConstraints.npm)
) {
packageFile.extractedConstraints.npm += ' <7';
npm = packageFile.extractedConstraints.npm + ' <7';
}
} else {
packageFile.extractedConstraints!.npm = '<7';
npm = '<7';
}
} else if (lockfileVersion === 2) {
if (packageFile.extractedConstraints?.npm) {
// Add a <9 constraint if the latest 8.x is compatible
if (
semver.satisfies('8.19.3', packageFile.extractedConstraints.npm)
) {
packageFile.extractedConstraints.npm += ' <9';
npm = packageFile.extractedConstraints.npm + ' <9';
}
} else {
packageFile.extractedConstraints!.npm = '<9';
npm = '<9';
}
}
if (npm) {
packageFile.extractedConstraints ??= {};
packageFile.extractedConstraints.npm = npm;
}

for (const dep of packageFile.deps) {
// TODO: types (#7154)
dep.lockedVersion = semver.valid(
Expand All @@ -96,9 +108,10 @@ export async function getLockedVersions(
}
const { lockfileVersion } = lockFileCache[pnpmShrinkwrap];
if (lockfileVersion) {
packageFile.extractedConstraints!.pnpm = getConstraints(
packageFile.extractedConstraints ??= {};
packageFile.extractedConstraints.pnpm = getConstraints(
lockfileVersion,
packageFile.extractedConstraints!.pnpm
packageFile.extractedConstraints.pnpm
);
}

Expand Down

0 comments on commit c146878

Please sign in to comment.