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); expect(testContent).toEqual(expected);
}); });
it.failing('replaces package', () => { it('replaces package', () => {
const upgrade = { const upgrade = {
depType: 'pnpm.catalog', depType: 'pnpm.catalog',
depName: 'config', depName: 'config',
newName: 'abc', newName: 'abc',
newValue: '2.0.0', newValue: '2.0.0',
managerData: {
catalogName: 'default',
},
}; };
const pnpmWorkspaceYaml = yamlCodeBlock` const pnpmWorkspaceYaml = yamlCodeBlock`
packages: packages:

View file

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