Skip to content

Commit f182708

Browse files
zharinovrarkins
andauthored
feat(npm): Support for new option replacementApproach (#34018)
Co-authored-by: Rhys Arkins <[email protected]>
1 parent 0361382 commit f182708

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

docs/usage/configuration-options.md

+10
Original file line numberDiff line numberDiff line change
@@ -3849,6 +3849,16 @@ The field supports multiple URLs but it is datasource-dependent on whether only
38493849

38503850
Add to this object if you wish to define rules that apply only to PRs that replace dependencies.
38513851

3852+
## replacementApproach
3853+
3854+
For `npm` manager when `replacementApproach=alias` then instead of replacing `"foo": "1.2.3"` with `"@my/foo": "1.2.4"` we would instead replace it with `"foo": "npm:@my/[email protected]"`.
3855+
3856+
```json
3857+
{
3858+
"replacementApproach": "alias"
3859+
}
3860+
```
3861+
38523862
## respectLatest
38533863

38543864
Similar to `ignoreUnstable`, this option controls whether to update to versions that are greater than the version tagged as `latest` in the repository.

lib/config/options/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,16 @@ const options: RenovateOptions[] = [
14651465
cli: false,
14661466
env: false,
14671467
},
1468+
{
1469+
name: 'replacementApproach',
1470+
description:
1471+
'Select whether to perform a direct replacement or alias replacement.',
1472+
type: 'string',
1473+
stage: 'branch',
1474+
allowedValues: ['replace', 'alias'],
1475+
supportedManagers: ['npm'],
1476+
default: 'replace',
1477+
},
14681478
{
14691479
name: 'matchConfidence',
14701480
description:

lib/modules/manager/npm/update/dependency/index.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as npmUpdater from '../..';
22
import { Fixtures } from '../../../../../../test/fixtures';
3+
import { type Upgrade } from '../../../types';
34

45
const readFixture = (x: string): string => Fixtures.get(x, '../..');
56

@@ -254,6 +255,23 @@ describe('modules/manager/npm/update/dependency/index', () => {
254255
expect(JSON.parse(testContent!).dependencies.abc).toBe('2.0.0');
255256
});
256257

258+
it('supports alias-based replacement', () => {
259+
const upgrade: Upgrade = {
260+
depType: 'dependencies',
261+
depName: 'config',
262+
newName: 'abc',
263+
replacementApproach: 'alias',
264+
newValue: '2.0.0',
265+
};
266+
const testContent = npmUpdater.updateDependency({
267+
fileContent: input01Content,
268+
upgrade,
269+
});
270+
expect(JSON.parse(testContent!).dependencies.config).toBe(
271+
272+
);
273+
});
274+
257275
it('replaces glob package resolutions', () => {
258276
const upgrade = {
259277
depType: 'dependencies',

lib/modules/manager/npm/update/dependency/index.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,38 @@ export function updateDependency({
161161
}
162162

163163
// TODO #22198
164-
let newFileContent = replaceAsString(
165-
parsedContents,
166-
fileContent,
167-
depType as NpmDepType,
168-
depName,
169-
oldVersion!,
170-
newValue!,
171-
overrideDepParents,
172-
);
173-
if (upgrade.newName) {
164+
let newFileContent: string;
165+
if (upgrade.newName && upgrade.replacementApproach === 'alias') {
174166
newFileContent = replaceAsString(
175167
parsedContents,
176-
newFileContent,
168+
fileContent,
177169
depType as NpmDepType,
178170
depName,
171+
oldVersion!,
172+
`npm:${upgrade.newName}@${newValue}`,
173+
overrideDepParents,
174+
);
175+
} else {
176+
newFileContent = replaceAsString(
177+
parsedContents,
178+
fileContent,
179+
depType as NpmDepType,
179180
depName,
180-
upgrade.newName,
181+
oldVersion!,
182+
newValue!,
181183
overrideDepParents,
182184
);
185+
if (upgrade.newName) {
186+
newFileContent = replaceAsString(
187+
parsedContents,
188+
newFileContent,
189+
depType as NpmDepType,
190+
depName,
191+
depName,
192+
upgrade.newName,
193+
overrideDepParents,
194+
);
195+
}
183196
}
184197
// istanbul ignore if
185198
if (!newFileContent) {

lib/modules/manager/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ export interface Upgrade<T = Record<string, any>> extends PackageDependency<T> {
192192
registryUrls?: string[] | null;
193193
currentVersion?: string;
194194
replaceString?: string;
195+
replacementApproach?: 'replace' | 'alias';
195196
}
196197

197198
export interface ArtifactNotice {

0 commit comments

Comments
 (0)