-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
78 lines (67 loc) · 2.4 KB
/
index.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
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
import { api as npm } from '../npm';
import type { NewValueConfig, VersioningApi } from '../types';
export const id = 'hashicorp';
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'];
function hashicorp2npm(input: string): string {
// The only case incompatible with semver is a "short" ~>, e.g. ~> 1.2
return input.replace(/~>(\s*\d+\.\d+$)/, '^$1').replace(',', '');
}
const isLessThanRange = (version: string, range: string): boolean =>
npm.isLessThanRange(hashicorp2npm(version), hashicorp2npm(range));
export const isValid = (input: string): string | boolean =>
input && npm.isValid(hashicorp2npm(input));
const matches = (version: string, range: string): boolean =>
npm.matches(hashicorp2npm(version), hashicorp2npm(range));
const getSatisfyingVersion = (versions: string[], range: string): string =>
npm.getSatisfyingVersion(versions.map(hashicorp2npm), hashicorp2npm(range));
const minSatisfyingVersion = (versions: string[], range: string): string =>
npm.minSatisfyingVersion(versions.map(hashicorp2npm), hashicorp2npm(range));
function getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion,
}: NewValueConfig): string {
if (rangeStrategy === 'replace') {
if (/~>\s*0\.\d+/.test(currentValue) && npm.getMajor(newVersion) === 0) {
const testFullVersion = /(~>\s*0\.)(\d+)\.\d$/;
let replaceValue = '';
if (testFullVersion.test(currentValue)) {
replaceValue = `$1${npm.getMinor(newVersion)}.0`;
} else {
replaceValue = `$1${npm.getMinor(newVersion)}$3`;
}
return currentValue.replace(/(~>\s*0\.)(\d+)(.*)$/, replaceValue);
}
// handle special ~> 1.2 case
if (/(~>\s*)\d+\.\d+$/.test(currentValue)) {
return currentValue.replace(
/(~>\s*)\d+\.\d+$/,
`$1${npm.getMajor(newVersion)}.0`
);
}
}
return npm.getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion,
});
}
export const api: VersioningApi = {
...npm,
isLessThanRange,
isValid,
matches,
getSatisfyingVersion,
minSatisfyingVersion,
getNewValue,
};
// eslint-disable-next-line @typescript-eslint/unbound-method
export const { isVersion } = api;
export default api;