mirror of
https://github.com/renovatebot/renovate.git
synced 2026-11-01 10:09:50 +00:00
fcb52fef2c
fix(manager/npm): match complete yarn lockfile selectors
44 lines
1.3 KiB
TypeScript
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`,
|
|
);
|
|
}
|