2020-05-01 16:03:48 +00:00
|
|
|
import { NewValueConfig, VersioningApi } from '../common';
|
2019-07-22 11:12:40 +00:00
|
|
|
import { api as npm } from '../npm';
|
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;
|
|
|
|
export const supportedRangeStrategies = ['bump', 'extend', 'pin', 'replace'];
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
function hashicorp2npm(input: string): string {
|
2018-10-06 07:43:25 +00:00
|
|
|
// The only case incompatible with semver is a "short" ~>, e.g. ~> 1.2
|
2018-10-06 11:59:44 +00:00
|
|
|
return input.replace(/~>(\s*\d+\.\d+$)/, '^$1').replace(',', '');
|
2018-10-06 07:43:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
const isLessThanRange = (version: string, range: string): boolean =>
|
2019-01-07 04:49:47 +00:00
|
|
|
npm.isLessThanRange(hashicorp2npm(version), hashicorp2npm(range));
|
2018-10-06 11:59:44 +00:00
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export const isValid = (input: string): string | boolean =>
|
2020-03-05 10:53:30 +00:00
|
|
|
input && npm.isValid(hashicorp2npm(input));
|
2018-10-06 11:59:44 +00:00
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
const matches = (version: string, range: string): boolean =>
|
2019-01-07 04:49:47 +00:00
|
|
|
npm.matches(hashicorp2npm(version), hashicorp2npm(range));
|
2018-10-06 07:43:25 +00:00
|
|
|
|
2020-12-10 08:25:04 +00:00
|
|
|
const getSatisfyingVersion = (versions: string[], range: string): string =>
|
|
|
|
npm.getSatisfyingVersion(versions.map(hashicorp2npm), hashicorp2npm(range));
|
2018-10-06 07:43:25 +00:00
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
const minSatisfyingVersion = (versions: string[], range: string): string =>
|
2019-01-07 04:49:47 +00:00
|
|
|
npm.minSatisfyingVersion(versions.map(hashicorp2npm), hashicorp2npm(range));
|
2018-10-06 07:43:25 +00:00
|
|
|
|
2020-01-16 12:43:58 +00:00
|
|
|
function getNewValue({
|
|
|
|
currentValue,
|
|
|
|
rangeStrategy,
|
|
|
|
fromVersion,
|
|
|
|
toVersion,
|
|
|
|
}: NewValueConfig): string {
|
2018-10-06 07:43:25 +00:00
|
|
|
// handle specia. ~> 1.2 case
|
2020-02-05 18:17:20 +00:00
|
|
|
if (/(~>\s*)\d+\.\d+$/.test(currentValue)) {
|
2018-10-06 07:43:25 +00:00
|
|
|
return currentValue.replace(
|
|
|
|
/(~>\s*)\d+\.\d+$/,
|
2019-01-07 04:49:47 +00:00
|
|
|
`$1${npm.getMajor(toVersion)}.0`
|
2018-10-06 07:43:25 +00:00
|
|
|
);
|
|
|
|
}
|
2020-01-16 12:43:58 +00:00
|
|
|
return npm.getNewValue({
|
|
|
|
currentValue,
|
|
|
|
rangeStrategy,
|
|
|
|
fromVersion,
|
|
|
|
toVersion,
|
|
|
|
});
|
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;
|