feat(npm): implement replacement for pnpm catalog updates

This commit is contained in:
Fotis Papadogeorgopoulos 2025-01-02 11:26:33 +02:00
parent ea5c14f003
commit e50bc2f9b5
No known key found for this signature in database
GPG key ID: 9E2557DCF51E95F8
2 changed files with 40 additions and 9 deletions

View file

@ -115,12 +115,15 @@ describe('modules/manager/npm/update/dependency/pnpm', () => {
expect(testContent).toEqual(expected);
});
it.failing('replaces package', () => {
it('replaces package', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'config',
newName: 'abc',
newValue: '2.0.0',
managerData: {
catalogName: 'default',
},
};
const pnpmWorkspaceYaml = yamlCodeBlock`
packages:

View file

@ -58,19 +58,47 @@ export function updatePnpmCatalogDependency({
return fileContent;
}
// TODO: handle depName === oldValue
// The old value is the name of the dependency itself
// Update the value
const path = getDepPath({
depName: depName!,
catalogName,
usesImplicitDefaultCatalog,
});
if (oldVersion === undefined) {
// There is some subtlety here
if (!document.hasIn(path)) {
return null;
}
if (catalogName === 'default' && usesImplicitDefaultCatalog) {
document.setIn(['catalog', depName], newValue);
} else {
document.setIn(['catalogs', catalogName, depName], newValue);
document.setIn(path, newValue);
// Update the name, for replacements
if (upgrade.newName) {
const newPath = getDepPath({
depName: upgrade.newName,
catalogName,
usesImplicitDefaultCatalog,
});
const oldValue = document.getIn(path);
document.deleteIn(path);
document.setIn(newPath, oldValue);
}
return stringify(document);
}
function getDepPath({
catalogName,
depName,
usesImplicitDefaultCatalog,
}: {
usesImplicitDefaultCatalog: boolean;
catalogName: string;
depName: string;
}): string[] {
if (catalogName === 'default' && usesImplicitDefaultCatalog) {
return ['catalog', depName];
} else {
return ['catalogs', catalogName, depName];
}
}