-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathcommon.ts
More file actions
114 lines (105 loc) · 2.94 KB
/
common.ts
File metadata and controls
114 lines (105 loc) · 2.94 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import * as semver from 'semver';
import { regEx } from '../../../util/regex.ts';
import { coerceString } from '../../../util/string.ts';
export function makeVersion(
version: string,
options: semver.RangeOptions,
): string | boolean | null {
const splitVersion = version.split('.');
const prerelease = semver.prerelease(version, options);
if (prerelease && !options.includePrerelease) {
if (!Number.isNaN(parseInt(prerelease.toString()[0], 10))) {
const stringVersion = `${splitVersion[0]}.${splitVersion[1]}.${splitVersion[2]}`;
return semver.valid(stringVersion, options);
}
return false;
}
if (
options.loose &&
!semver.valid(version, options) &&
splitVersion.length !== 3
) {
return semver.valid(semver.coerce(version, options), options);
}
return semver.valid(version, options);
}
export function cleanVersion(version: string): string {
if (version) {
return version
.replace(
regEx(
/,|\[|\]|"|include_prerelease=|include_prerelease|loose=|True|False/g,
),
'',
)
.trim();
}
return version;
}
export function getOptions(input: string): {
loose: boolean;
includePrerelease: boolean;
} {
let includePrerelease = false;
let loose = true;
if (input) {
includePrerelease =
input.includes('include_prerelease') &&
!input.includes('include_prerelease=False');
loose = input.includes('loose=True') || !input.includes('loose=False');
}
return { loose, includePrerelease };
}
export function containsOperators(input: string): boolean {
return regEx('[<=>^~]').test(input);
}
export function matchesWithOptions(
version: string,
cleanRange: string,
options: semver.RangeOptions,
): boolean {
let cleanedVersion = version;
if (
cleanedVersion &&
semver.prerelease(cleanedVersion) &&
options.includePrerelease
) {
const coercedVersion = semver.coerce(cleanedVersion)?.raw;
cleanedVersion = coerceString(coercedVersion);
}
return semver.satisfies(cleanedVersion, cleanRange, options);
}
export function findSatisfyingVersion(
versions: string[],
range: string,
compareRt: number,
): string | null {
const options = getOptions(range);
let cur: any = null;
let curSV: any = null;
let index = 0;
let curIndex = -1;
for (const v of versions) {
const versionFromList = makeVersion(v, options);
if (typeof versionFromList === 'string') {
const cleanedVersion = cleanVersion(versionFromList);
const options = getOptions(range);
const cleanRange = cleanVersion(range);
if (matchesWithOptions(cleanedVersion, cleanRange, options)) {
if (
!cur ||
semver.compare(curSV, versionFromList, options) === compareRt
) {
cur = versionFromList;
curIndex = index;
curSV = new semver.SemVer(cur, options);
}
}
}
index += 1;
}
if (curIndex >= 0) {
return versions[curIndex];
}
return null;
}