2023-01-09 15:23:53 +00:00
|
|
|
import { logger } from '../../../logger';
|
2022-03-03 09:35:26 +00:00
|
|
|
import type { RangeStrategy } from '../../../types/versioning';
|
2023-07-25 18:18:41 +00:00
|
|
|
import { getExcludedVersions, getFilteredRange } from '../common';
|
2019-07-22 11:12:40 +00:00
|
|
|
import { api as npm } from '../npm';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { NewValueConfig, VersioningApi } from '../types';
|
2023-01-07 14:44:38 +00:00
|
|
|
import { hashicorp2npm, npm2hashicorp } from './convertor';
|
2018-10-06 07:43:25 +00:00
|
|
|
|
2020-02-18 07:34:10 +00:00
|
|
|
export const id = 'hashicorp';
|
2020-02-17 22:02:00 +00:00
|
|
|
export const displayName = 'Hashicorp';
|
|
|
|
export const urls = [
|
|
|
|
'https://www.terraform.io/docs/configuration/terraform.html#specifying-a-required-terraform-version',
|
|
|
|
];
|
|
|
|
export const supportsRanges = true;
|
2022-02-27 16:54:56 +00:00
|
|
|
export const supportedRangeStrategies: RangeStrategy[] = [
|
|
|
|
'bump',
|
|
|
|
'widen',
|
|
|
|
'pin',
|
|
|
|
'replace',
|
|
|
|
];
|
2020-02-17 22:02:00 +00:00
|
|
|
|
2022-02-06 09:14:04 +00:00
|
|
|
function isLessThanRange(version: string, range: string): boolean {
|
2023-01-07 14:44:38 +00:00
|
|
|
return !!npm.isLessThanRange?.(version, hashicorp2npm(range));
|
2022-02-06 09:14:04 +00:00
|
|
|
}
|
2018-10-06 11:59:44 +00:00
|
|
|
|
2023-01-07 14:44:38 +00:00
|
|
|
export function isValid(input: string): boolean {
|
|
|
|
if (input) {
|
|
|
|
try {
|
|
|
|
return npm.isValid(hashicorp2npm(input));
|
|
|
|
} catch (err) {
|
2023-01-10 13:28:03 +00:00
|
|
|
logger.trace({ value: input }, 'Unsupported hashicorp versioning value');
|
2023-01-07 14:44:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-06 11:59:44 +00:00
|
|
|
|
2023-01-07 14:44:38 +00:00
|
|
|
function matches(version: string, range: string): boolean {
|
2023-07-25 18:18:41 +00:00
|
|
|
const excludedVersions = getExcludedVersions(range);
|
|
|
|
if (excludedVersions.includes(version)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const filteredRange = getFilteredRange(range);
|
|
|
|
return (
|
|
|
|
isValid(filteredRange) && npm.matches(version, hashicorp2npm(filteredRange))
|
|
|
|
);
|
2023-01-07 14:44:38 +00:00
|
|
|
}
|
2018-10-06 07:43:25 +00:00
|
|
|
|
2022-02-06 09:14:04 +00:00
|
|
|
function getSatisfyingVersion(
|
|
|
|
versions: string[],
|
|
|
|
range: string
|
|
|
|
): string | null {
|
2023-07-25 18:18:41 +00:00
|
|
|
const excludedVersions = getExcludedVersions(range);
|
|
|
|
const filteredRange = getFilteredRange(range);
|
|
|
|
const filteredVersions = versions.filter(
|
|
|
|
(version) => !excludedVersions.includes(version)
|
|
|
|
);
|
|
|
|
|
|
|
|
return npm.getSatisfyingVersion(
|
|
|
|
filteredVersions,
|
|
|
|
hashicorp2npm(filteredRange)
|
|
|
|
);
|
2022-02-06 09:14:04 +00:00
|
|
|
}
|
2018-10-06 07:43:25 +00:00
|
|
|
|
2022-02-06 09:14:04 +00:00
|
|
|
function minSatisfyingVersion(
|
|
|
|
versions: string[],
|
|
|
|
range: string
|
|
|
|
): string | null {
|
2023-07-25 18:18:41 +00:00
|
|
|
const excludedVersions = getExcludedVersions(range);
|
|
|
|
const filteredRange = getFilteredRange(range);
|
|
|
|
const filteredVersions = versions.filter(
|
|
|
|
(version) => !excludedVersions.includes(version)
|
|
|
|
);
|
|
|
|
return npm.minSatisfyingVersion(
|
|
|
|
filteredVersions,
|
|
|
|
hashicorp2npm(filteredRange)
|
|
|
|
);
|
2022-02-06 09:14:04 +00:00
|
|
|
}
|
2018-10-06 07:43:25 +00:00
|
|
|
|
2020-01-16 12:43:58 +00:00
|
|
|
function getNewValue({
|
|
|
|
currentValue,
|
|
|
|
rangeStrategy,
|
2021-02-16 09:52:05 +00:00
|
|
|
currentVersion,
|
2021-02-16 11:33:44 +00:00
|
|
|
newVersion,
|
2022-02-06 09:14:04 +00:00
|
|
|
}: NewValueConfig): string | null {
|
2022-10-18 14:14:02 +00:00
|
|
|
let npmNewVersion = npm.getNewValue({
|
2023-01-07 14:44:38 +00:00
|
|
|
currentValue: hashicorp2npm(currentValue),
|
2020-01-16 12:43:58 +00:00
|
|
|
rangeStrategy,
|
2021-02-16 09:52:05 +00:00
|
|
|
currentVersion,
|
2021-02-16 11:33:44 +00:00
|
|
|
newVersion,
|
2020-01-16 12:43:58 +00:00
|
|
|
});
|
2023-01-07 14:44:38 +00:00
|
|
|
if (npmNewVersion) {
|
|
|
|
npmNewVersion = npm2hashicorp(npmNewVersion);
|
|
|
|
if (currentValue.startsWith('v') && !npmNewVersion.startsWith('v')) {
|
|
|
|
npmNewVersion = `v${npmNewVersion}`;
|
|
|
|
}
|
2022-10-18 14:14:02 +00:00
|
|
|
}
|
|
|
|
return npmNewVersion;
|
2018-10-06 07:43:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 11:12:40 +00:00
|
|
|
export const api: VersioningApi = {
|
2019-01-07 04:49:47 +00:00
|
|
|
...npm,
|
2018-10-06 11:59:44 +00:00
|
|
|
isLessThanRange,
|
|
|
|
isValid,
|
2018-10-06 07:43:25 +00:00
|
|
|
matches,
|
2020-12-10 08:25:04 +00:00
|
|
|
getSatisfyingVersion,
|
2018-10-06 07:43:25 +00:00
|
|
|
minSatisfyingVersion,
|
|
|
|
getNewValue,
|
|
|
|
};
|
2019-07-22 11:12:40 +00:00
|
|
|
|
2020-08-10 14:18:08 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
2020-02-05 18:17:20 +00:00
|
|
|
export const { isVersion } = api;
|
2019-07-22 11:12:40 +00:00
|
|
|
|
|
|
|
export default api;
|