-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathutils.ts
More file actions
79 lines (70 loc) · 2.25 KB
/
utils.ts
File metadata and controls
79 lines (70 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import is from '@sindresorhus/is';
import { getRangeStrategy } from '../../../../modules/manager';
import type { LookupUpdate } from '../../../../modules/manager/types';
import * as allVersioning from '../../../../modules/versioning';
import * as template from '../../../../util/template';
import type { LookupUpdateConfig } from './types';
export function addReplacementUpdateIfValid(
updates: LookupUpdate[],
config: LookupUpdateConfig,
): void {
const replacementNewName = determineNewReplacementName(config);
const replacementNewValue = determineNewReplacementValue(config);
if (
config.packageName !== replacementNewName ||
config.currentValue !== replacementNewValue
) {
updates.push({
updateType: 'replacement',
newName: replacementNewName,
newValue: replacementNewValue!,
});
}
}
export function isReplacementRulesConfigured(
config: LookupUpdateConfig,
): boolean {
return (
is.nonEmptyString(config.replacementName) ||
is.nonEmptyString(config.replacementNameTemplate) ||
is.nonEmptyString(config.replacementVersion) ||
is.nonEmptyString(config.replacementVersionTemplate)
);
}
export function determineNewReplacementName(
config: LookupUpdateConfig,
): string {
if (config.replacementName) {
return config.replacementName;
}
if (config.replacementNameTemplate) {
return template.compile(config.replacementNameTemplate, config, true);
}
return config.packageName;
}
export function determineNewReplacementValue(
config: LookupUpdateConfig,
): string | undefined | null {
const newVersion = getNewVersion(config);
if (!newVersion) {
return config.currentValue;
}
const versioningApi = allVersioning.get(config.versioning);
const rangeStrategy = getRangeStrategy(config);
return versioningApi.getNewValue({
// TODO #22198
currentValue: config.currentValue!,
newVersion,
rangeStrategy: rangeStrategy!,
isReplacement: true,
});
}
function getNewVersion(config: LookupUpdateConfig): string | null {
if (!is.nullOrUndefined(config.replacementVersion)) {
return config.replacementVersion;
}
if (!is.nullOrUndefined(config.replacementVersionTemplate)) {
return template.compile(config.replacementVersionTemplate, config, true);
}
return null;
}