2021-08-30 08:25:20 +00:00
|
|
|
import is from '@sindresorhus/is';
|
2021-02-18 08:21:01 +00:00
|
|
|
import { logger } from '../../../../logger';
|
2022-03-03 09:35:26 +00:00
|
|
|
import type { VersioningApi } from '../../../../modules/versioning/types';
|
2021-10-27 14:37:11 +00:00
|
|
|
import { regEx } from '../../../../util/regex';
|
2021-02-18 08:21:01 +00:00
|
|
|
|
|
|
|
export function getCurrentVersion(
|
2021-11-23 08:13:17 +00:00
|
|
|
currentValue: string,
|
|
|
|
lockedVersion: string,
|
2024-10-08 15:24:46 +00:00
|
|
|
versioningApi: VersioningApi,
|
2021-02-18 08:21:01 +00:00
|
|
|
rangeStrategy: string,
|
|
|
|
latestVersion: string,
|
2023-11-07 15:50:29 +00:00
|
|
|
allVersions: string[],
|
2021-02-18 08:21:01 +00:00
|
|
|
): string | null {
|
2021-08-30 08:25:20 +00:00
|
|
|
// istanbul ignore if
|
|
|
|
if (!is.string(currentValue)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-18 08:21:01 +00:00
|
|
|
logger.trace(`currentValue ${currentValue} is range`);
|
2024-03-10 12:09:10 +00:00
|
|
|
if (allVersions.includes(currentValue)) {
|
|
|
|
return currentValue;
|
|
|
|
}
|
2021-02-18 08:28:18 +00:00
|
|
|
let useVersions = allVersions.filter((v) =>
|
2024-10-08 15:24:46 +00:00
|
|
|
versioningApi.matches(v, currentValue),
|
2021-02-18 08:28:18 +00:00
|
|
|
);
|
2024-02-22 16:06:50 +00:00
|
|
|
if (useVersions.length === 1) {
|
|
|
|
return useVersions[0];
|
|
|
|
}
|
2024-10-08 15:24:46 +00:00
|
|
|
if (latestVersion && versioningApi.matches(latestVersion, currentValue)) {
|
2021-02-18 08:21:01 +00:00
|
|
|
useVersions = useVersions.filter(
|
2024-10-08 15:24:46 +00:00
|
|
|
(v) => !versioningApi.isGreaterThan(v, latestVersion),
|
2021-02-18 08:21:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (rangeStrategy === 'pin') {
|
|
|
|
return (
|
2021-02-18 08:28:18 +00:00
|
|
|
lockedVersion ||
|
2024-10-08 15:24:46 +00:00
|
|
|
versioningApi.getSatisfyingVersion(useVersions, currentValue)
|
2021-02-18 08:21:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (rangeStrategy === 'bump') {
|
|
|
|
// Use the lowest version in the current range
|
2024-10-08 15:24:46 +00:00
|
|
|
return versioningApi.minSatisfyingVersion(useVersions, currentValue);
|
2021-02-18 08:21:01 +00:00
|
|
|
}
|
|
|
|
// Use the highest version in the current range
|
2024-10-08 15:24:46 +00:00
|
|
|
const satisfyingVersion = versioningApi.getSatisfyingVersion(
|
2024-02-18 08:52:03 +00:00
|
|
|
useVersions,
|
|
|
|
currentValue,
|
|
|
|
);
|
|
|
|
if (satisfyingVersion) {
|
|
|
|
return satisfyingVersion;
|
|
|
|
}
|
|
|
|
|
2024-10-08 15:24:46 +00:00
|
|
|
if (versioningApi.isVersion(currentValue)) {
|
2024-02-18 08:52:03 +00:00
|
|
|
return currentValue;
|
|
|
|
}
|
2024-10-08 15:24:46 +00:00
|
|
|
if (versioningApi.isSingleVersion(currentValue)) {
|
2024-02-18 08:52:03 +00:00
|
|
|
return currentValue.replace(regEx(/=/g), '').trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2021-02-18 08:21:01 +00:00
|
|
|
}
|