Skip to content
Snippets Groups Projects
Unverified Commit c1468787 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(npm): Avoid usage of non-null assertion for constraints (#22405)

parent 71ce657a
No related branches found
No related tags found
No related merge requests found
......@@ -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[
......@@ -57,17 +62,19 @@ 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) {
......@@ -75,12 +82,17 @@ export async function getLockedVersions(
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(
......@@ -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
);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment