-
Notifications
You must be signed in to change notification settings - Fork 8
/
builds.ts
156 lines (137 loc) · 4.2 KB
/
builds.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import is from '@sindresorhus/is';
import {
ReleaseResult,
getPkgReleases,
} from 'renovate/dist/modules/datasource';
import { get as getVersioning } from 'renovate/dist/modules/versioning';
import { add as addHostRule } from 'renovate/dist/util/host-rules';
import { getRegexPredicate } from 'renovate/dist/util/string-match';
import * as semver from 'semver';
import log from './logger';
import * as renovate from './renovate';
renovate.register();
let latestStable: string | undefined;
export { addHostRule };
function getVersions(versions: string[]): ReleaseResult {
return {
releases: versions.map((version) => ({
version,
})),
};
}
export interface BuildsConfig {
allowedVersions?: string;
datasource: string;
depName: string;
forceUnstable?: boolean;
ignoredVersions: string[];
maxVersions?: number;
lastOnly: boolean;
latestVersion?: string;
lookupName?: string;
startVersion: string;
versioning: string;
versions?: string[];
extractVersion?: string;
}
export interface BuildsResult {
latestStable: string | undefined;
versions: string[];
}
export async function getBuildList({
allowedVersions,
datasource,
depName,
lookupName,
versioning,
startVersion,
ignoredVersions,
lastOnly,
forceUnstable,
versions,
latestVersion,
maxVersions,
extractVersion,
}: BuildsConfig): Promise<BuildsResult | null> {
log('Looking up versions');
const ver = getVersioning(versioning);
const pkgResult = versions
? getVersions(versions)
: await getPkgReleases({
datasource,
packageName: lookupName ?? depName,
versioning,
extractVersion,
});
if (!pkgResult) {
return null;
}
let allVersions = pkgResult.releases
.map((v) => v.version)
.filter((v) => ver.isVersion(v) && ver.isCompatible(v, startVersion));
// filter duplicate versions (16.0.2+7 == 16.0.2+8)
allVersions = allVersions
.reverse()
.filter((v, i) => allVersions.findIndex((f) => ver.equals(f, v)) === i)
.reverse();
log(`Found ${allVersions.length} total versions`);
if (!allVersions.length) {
return null;
}
allVersions = allVersions
.filter((v) => v === startVersion || ver.isGreaterThan(v, startVersion))
.filter((v) => !ignoredVersions.includes(v));
if (!forceUnstable) {
log('Filter unstable versions');
allVersions = allVersions.filter((v) => ver.isStable(v));
}
if (is.string(allowedVersions)) {
const isAllowedPred = getRegexPredicate(allowedVersions);
if (isAllowedPred) {
allVersions = allVersions.filter((version) => isAllowedPred(version));
} else if (ver.isValid(allowedVersions)) {
allVersions = allVersions.filter((version) =>
ver.matches(version, allowedVersions),
);
} else if (semver.validRange(allowedVersions)) {
allVersions = allVersions.filter((v) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
semver.satisfies(semver.coerce(v)!, allowedVersions),
);
} else {
log.warn(`Invalid 'allowedVersions' options: ${allowedVersions}`);
return null;
}
}
if (!allVersions.length) {
log('Nothing to build');
return null;
}
log(`Found ${allVersions.length} versions within our range`);
log(`Candidates:`, allVersions.join(', '));
latestStable =
latestVersion ??
/* istanbul ignore next: not testable ts */
pkgResult.tags?.latest ??
allVersions.filter((v) => ver.isStable(v)).pop();
log('Latest stable version is', latestStable);
if (latestStable && !allVersions.includes(latestStable)) {
log.warn(
`LatestStable '${latestStable}' not buildable, candidates:`,
allVersions.join(', '),
);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const lastVersion = allVersions.at(-1)!;
log('Most recent version is', lastVersion);
if (is.number(maxVersions) && maxVersions > 0) {
log(`Building last ${maxVersions} version only`);
allVersions = allVersions.slice(-maxVersions);
}
if (lastOnly) {
log('Building last version only');
allVersions = [latestStable && !forceUnstable ? latestStable : lastVersion];
}
log('Build list:', allVersions.join(', '));
return { versions: allVersions, latestStable };
}