0
0
Fork 0
mirror of https://github.com/renovatebot/renovate.git synced 2025-02-06 01:20:45 +00:00
renovatebot_renovate/lib/modules/manager/npm/update/dependency/pnpm.ts
Fotis Papadogeorgopoulos 0f06866080
feat(managers/npm): support pnpm catalogs (#33376)
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
2025-01-28 10:11:55 +00:00

157 lines
4.8 KiB
TypeScript

import is from '@sindresorhus/is';
import type { Document } from 'yaml';
import { CST, isCollection, isPair, isScalar, parseDocument } from 'yaml';
import { logger } from '../../../../../logger';
import type { UpdateDependencyConfig } from '../../../types';
import { PnpmCatalogsSchema } from '../../schema';
import { getNewGitValue, getNewNpmAliasValue } from './common';
export function updatePnpmCatalogDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string | null {
const { depType, managerData, depName } = upgrade;
const catalogName = depType?.split('.').at(-1);
// istanbul ignore if
if (!is.string(catalogName)) {
logger.error(
'No catalogName was found; this is likely an extraction error.',
);
return null;
}
let { newValue } = upgrade;
newValue = getNewGitValue(upgrade) ?? newValue;
newValue = getNewNpmAliasValue(newValue, upgrade) ?? newValue;
logger.trace(
`npm.updatePnpmCatalogDependency(): ${depType}:${managerData?.catalogName}.${depName} = ${newValue}`,
);
let document;
let parsedContents;
try {
// In order to preserve the original formatting as much as possible, we want
// manipulate the CST directly. Using the AST (the result of parseDocument)
// does not guarantee that formatting would be the same after
// stringification. However, the CST is more annoying to query for certain
// values. Thus, we use both an annotated AST and a JS representation; the
// former for manipulation, and the latter for querying/validation.
document = parseDocument(fileContent, { keepSourceTokens: true });
parsedContents = PnpmCatalogsSchema.parse(document.toJS());
} catch (err) {
logger.debug({ err }, 'Could not parse pnpm-workspace YAML file.');
return null;
}
// In pnpm-workspace.yaml, the default catalog can be either `catalog` or
// `catalog.default`, but not both (pnpm throws outright with a config error).
// Thus, we must check which entry is being used, to reference it from / set
// it in the right place.
const usesImplicitDefaultCatalog = parsedContents.catalog !== undefined;
const oldVersion =
catalogName === 'default' && usesImplicitDefaultCatalog
? parsedContents.catalog?.[depName!]
: parsedContents.catalogs?.[catalogName]?.[depName!];
if (oldVersion === newValue) {
logger.trace('Version is already updated');
return fileContent;
}
// Update the value
const path = getDepPath({
depName: depName!,
catalogName,
usesImplicitDefaultCatalog,
});
const modifiedDocument = changeDependencyIn(document, path, {
newValue,
newName: upgrade.newName,
});
if (!modifiedDocument) {
// Case where we are explicitly unable to substitute the key/value, for
// example if the value was an alias.
return null;
}
// istanbul ignore if: this should not happen in practice, but we must satisfy th etypes
if (!modifiedDocument.contents?.srcToken) {
return null;
}
return CST.stringify(modifiedDocument.contents.srcToken);
}
/**
* Change the scalar name and/or value of a collection item in a YAML document,
* while keeping formatting consistent. Mutates the given document.
*/
function changeDependencyIn(
document: Document,
path: string[],
{ newName, newValue }: { newName?: string; newValue?: string },
): Document | null {
const parentPath = path.slice(0, -1);
const relevantItemKey = path.at(-1);
const parentNode = document.getIn(parentPath);
if (!parentNode || !isCollection(parentNode)) {
return null;
}
const relevantNode = parentNode.items.find(
(item) =>
isPair(item) && isScalar(item.key) && item.key.value === relevantItemKey,
);
if (!relevantNode || !isPair(relevantNode)) {
return null;
}
if (newName) {
// istanbul ignore if: the try..catch block above already throws if a key is an alias
if (!CST.isScalar(relevantNode.srcToken?.key)) {
return null;
}
CST.setScalarValue(relevantNode.srcToken.key, newName);
}
if (newValue) {
// We only support scalar values when substituting. This explicitly avoids
// substituting aliases, since those can be resolved from a shared location,
// and replacing either the referrent anchor or the alias would be wrong in
// the general case. We leave this up to the user, e.g. via a Regex custom
// manager.
if (!CST.isScalar(relevantNode.srcToken?.value)) {
return null;
}
CST.setScalarValue(relevantNode.srcToken.value, newValue);
}
return 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];
}
}