Files
Sergei Zharinov fcb52fef2c fix(manager/npm): prevent updates to unrelated Yarn v1 entries (#45737)
fix(manager/npm): match complete yarn lockfile selectors
2026-09-07 12:14:09 +00:00

44 lines
1.3 KiB
TypeScript

import { logger } from '../../../../../../logger/index.ts';
import { regEx } from '../../../../../../util/regex.ts';
export function replaceConstraintVersion(
lockFileContent: string,
depName: string,
constraint: string,
newVersion: string,
newConstraint?: string,
): string {
if (lockFileContent.startsWith('__metadata:')) {
// Yarn 2+
return lockFileContent;
}
const depNameConstraint = `${depName}@${constraint}`;
const escaped = depNameConstraint.replace(
regEx(/(?<special>@|\^|\.|\\|\|)/g),
'\\$<special>',
);
const matchString = `((?:^|\n|, )"?${escaped}(("|",|,)[^\n:]*)?:\n)(.*\n)*?(\\s+dependencies|\n[@a-z])`;
// yarn will fill in the details later
const matchResult = regEx(matchString).exec(lockFileContent);
/* v8 ignore next -- needs test */
if (!matchResult) {
logger.debug(
{ depName, constraint, newVersion },
'Could not find constraint in lock file',
);
return lockFileContent;
}
let constraintLine = matchResult[1];
if (newConstraint) {
const newDepNameConstraint = `${depName}@${newConstraint}`;
constraintLine = constraintLine.replace(
depNameConstraint,
newDepNameConstraint,
);
}
return lockFileContent.replace(
regEx(matchString),
`${constraintLine} version "${newVersion}"\n$5`,
);
}