/
current.ts
45 lines (44 loc) · 1.39 KB
/
current.ts
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
import is from '@sindresorhus/is';
import { logger } from '../../../../logger';
import { regEx } from '../../../../util/regex';
import type { VersioningApi } from '../../../../versioning/types';
export function getCurrentVersion(
currentValue: string,
lockedVersion: string,
versioning: VersioningApi,
rangeStrategy: string,
latestVersion: string,
allVersions: string[]
): string | null {
// istanbul ignore if
if (!is.string(currentValue)) {
return null;
}
if (versioning.isVersion(currentValue)) {
return currentValue;
}
if (versioning.isSingleVersion(currentValue)) {
return currentValue.replace(regEx(/=/g), '').trim();
}
logger.trace(`currentValue ${currentValue} is range`);
let useVersions = allVersions.filter((v) =>
versioning.matches(v, currentValue)
);
if (latestVersion && versioning.matches(latestVersion, currentValue)) {
useVersions = useVersions.filter(
(v) => !versioning.isGreaterThan(v, latestVersion)
);
}
if (rangeStrategy === 'pin') {
return (
lockedVersion ||
versioning.getSatisfyingVersion(useVersions, currentValue)
);
}
if (rangeStrategy === 'bump') {
// Use the lowest version in the current range
return versioning.minSatisfyingVersion(useVersions, currentValue);
}
// Use the highest version in the current range
return versioning.getSatisfyingVersion(useVersions, currentValue);
}