import { isString } from '@sindresorhus/is'; import type { ReleaseType } from 'semver'; import semver from 'semver'; import { XmlDocument } from 'xmldoc'; import { logger } from '../../../logger/index.ts'; import { replaceAt } from '../../../util/string.ts'; import type { BumpPackageVersionResult, UpdateDependencyConfig, Upgrade, } from '../types.ts'; export function updateAtPosition( fileContent: string, upgrade: Upgrade, endingAnchor: string, ): string | null { const { depName, newName, currentValue, newValue, fileReplacePosition } = upgrade; let leftPart = fileContent.slice(0, fileReplacePosition); const rightPart = fileContent.slice(fileReplacePosition); const versionClosePosition = rightPart.indexOf(endingAnchor); let restPart = rightPart.slice(versionClosePosition); const versionPart = rightPart.slice(0, versionClosePosition); const version = versionPart.trim(); if (newName) { const blockStart = Math.max( leftPart.lastIndexOf(' 0) { leftBlock = updateValue(leftBlock, 'groupId', groupId, newGroupId); } else { rightBlock = updateValue(rightBlock, 'groupId', groupId, newGroupId); } if (leftBlock.indexOf(' 0) { leftBlock = updateValue( leftBlock, 'artifactId', artifactId, newArtifactId, ); } else { rightBlock = updateValue( rightBlock, 'artifactId', artifactId, newArtifactId, ); } leftPart = leftPart.slice(0, blockStart) + leftBlock; restPart = rightBlock + restPart.slice(blockEnd); } else if (version === newValue) { return fileContent; } if (version === currentValue || upgrade.sharedVariableName) { // TODO: validate newValue (#22198) const replacedPart = versionPart.replace(version, newValue!); return leftPart + replacedPart + restPart; } if ( upgrade.datasource === 'docker' || upgrade.datasource === 'buildpacks-registry' ) { // In contrast to maven dependencies, cloud native buildpacks are not contained in specific version tags. // Instead they are contained in the value of the buildpack tag and we have to update it differently. let replacedPart = version; if (currentValue) { replacedPart = version.replace(currentValue, newValue!); } if (upgrade.currentDigest && upgrade.newDigest) { replacedPart = replacedPart.replace( upgrade.currentDigest, upgrade.newDigest, ); } if (replacedPart !== version) { return leftPart + replacedPart + restPart; } } logger.debug({ depName, version, currentValue, newValue }, 'Unknown value'); return null; } export function updateDependency({ fileContent, upgrade, }: UpdateDependencyConfig): string | null { const offset = fileContent.indexOf('<'); const spaces = fileContent.slice(0, offset); const restContent = fileContent.slice(offset); const updatedContent = updateAtPosition(restContent, upgrade, '', elementStart) + 1; const end = content.indexOf(`