0
0
Fork 0
mirror of https://github.com/renovatebot/renovate.git synced 2025-05-13 16:11:49 +00:00
renovatebot_renovate/lib/modules/manager/npm/update/locked-dependency/yarn-lock/replace.ts
2023-11-07 15:50:29 +00:00

40 lines
1.2 KiB
TypeScript

import { logger } from '../../../../../../logger';
import { regEx } from '../../../../../../util/regex';
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(/(@|\^|\.|\\|\|)/g, '\\$1');
const matchString = `(${escaped}(("|",|,)[^\n:]*)?:\n)(.*\n)*?(\\s+dependencies|\n[@a-z])`;
// yarn will fill in the details later
const matchResult = regEx(matchString).exec(lockFileContent);
// istanbul ignore if
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`,
);
}