-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathindex.ts
298 lines (265 loc) · 8.11 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import semver from 'semver';
import { parseRange } from 'semver-utils';
import { logger } from '../../../logger';
import type { RangeStrategy } from '../../../types/versioning';
import { regEx } from '../../../util/regex';
import { api as npm } from '../npm';
import type { NewValueConfig, VersioningApi } from '../types';
export const id = 'composer';
export const displayName = 'Composer';
export const urls = [
'https://getcomposer.org/doc/articles/versions.md',
'https://packagist.org/packages/composer/semver',
'https://madewithlove.be/tilde-and-caret-constraints/',
'https://semver.mwl.be',
];
export const supportsRanges = true;
export const supportedRangeStrategies: RangeStrategy[] = [
'bump',
'widen',
'pin',
'replace',
'update-lockfile',
];
function getVersionParts(input: string): [string, string] {
const versionParts = input.split('-');
if (versionParts.length === 1) {
return [input, ''];
}
return [versionParts[0], '-' + versionParts[1]];
}
function padZeroes(input: string): string {
const [output, stability] = getVersionParts(input);
const sections = output.split('.');
while (sections.length < 3) {
sections.push('0');
}
return sections.join('.') + stability;
}
function convertStabilityModifier(input: string): string {
// Handle stability modifiers.
const versionParts = input.split('@');
if (versionParts.length === 1) {
return input;
}
// 1.0@beta2 to 1.0-beta.2
const stability = versionParts[1].replace(
regEx(/(?:^|\s)(beta|alpha|rc)([1-9][0-9]*)(?: |$)/gi),
'$1.$2'
);
// If there is a stability part, npm semver expects the version
// to be full
return padZeroes(versionParts[0]) + '-' + stability;
}
function normalizeVersion(input: string): string {
let output = input;
output = output.replace(regEx(/(^|>|>=|\^|~)v/i), '$1');
return convertStabilityModifier(output);
}
function composer2npm(input: string): string {
const cleanInput = normalizeVersion(input);
if (npm.isVersion(cleanInput)) {
return cleanInput;
}
if (npm.isVersion(padZeroes(cleanInput))) {
return padZeroes(cleanInput);
}
const [versionId, stability] = getVersionParts(cleanInput);
let output = versionId;
// ~4 to ^4 and ~4.1 to ^4.1
output = output.replace(
regEx(/(?:^|\s)~([1-9][0-9]*(?:\.[0-9]*)?)(?: |$)/g),
'^$1'
);
// ~0.4 to >=0.4 <1
output = output.replace(
regEx(/(?:^|\s)~(0\.[1-9][0-9]*)(?: |$)/g),
'>=$1 <1'
);
return output + stability;
}
function equals(a: string, b: string): boolean {
return npm.equals(composer2npm(a), composer2npm(b));
}
function getMajor(version: string): number | null {
const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getMajor(semverVersion) : null;
}
function getMinor(version: string): number | null {
const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getMinor(semverVersion) : null;
}
function getPatch(version: string): number | null {
const semverVersion = semver.coerce(composer2npm(version));
return semverVersion ? npm.getPatch(semverVersion) : null;
}
function isGreaterThan(a: string, b: string): boolean {
return npm.isGreaterThan(composer2npm(a), composer2npm(b));
}
function isLessThanRange(version: string, range: string): boolean {
return !!npm.isLessThanRange?.(composer2npm(version), composer2npm(range));
}
function isSingleVersion(input: string): boolean {
return !!input && npm.isSingleVersion(composer2npm(input));
}
function isStable(version: string): boolean {
return !!(version && npm.isStable(composer2npm(version)));
}
export function isValid(input: string): boolean {
return !!input && npm.isValid(composer2npm(input));
}
export function isVersion(input: string): boolean {
return !!input && npm.isVersion(composer2npm(input));
}
function matches(version: string, range: string): boolean {
return npm.matches(composer2npm(version), composer2npm(range));
}
function getSatisfyingVersion(
versions: string[],
range: string
): string | null {
return npm.getSatisfyingVersion(
versions.map(composer2npm),
composer2npm(range)
);
}
function minSatisfyingVersion(
versions: string[],
range: string
): string | null {
return npm.minSatisfyingVersion(
versions.map(composer2npm),
composer2npm(range)
);
}
function getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion,
}: NewValueConfig): string | null {
if (rangeStrategy === 'pin') {
return newVersion;
}
if (rangeStrategy === 'update-lockfile') {
if (matches(newVersion, currentValue)) {
return currentValue;
}
return getNewValue({
currentValue,
rangeStrategy: 'replace',
currentVersion,
newVersion,
});
}
const currentMajor = currentVersion ? getMajor(currentVersion) : null;
const toMajor = getMajor(newVersion);
const toMinor = getMinor(newVersion);
let newValue: string | null = null;
if (isVersion(currentValue)) {
newValue = newVersion;
} else if (regEx(/^[~^](0\.[1-9][0-9]*)$/).test(currentValue)) {
const operator = currentValue.substr(0, 1);
// handle ~0.4 case first
if (toMajor === 0) {
newValue = `${operator}0.${toMinor}`;
} else {
newValue = `${operator}${toMajor}.0`;
}
} else if (regEx(/^[~^]([0-9]*)$/).test(currentValue)) {
// handle ~4 case
const operator = currentValue.substr(0, 1);
newValue = `${operator}${toMajor}`;
} else if (
toMajor &&
regEx(/^[~^]([0-9]*(?:\.[0-9]*)?)$/).test(currentValue)
) {
const operator = currentValue.substr(0, 1);
// handle ~4.1 case
if ((currentMajor && toMajor > currentMajor) || !toMinor) {
newValue = `${operator}${toMajor}.0`;
} else {
newValue = `${operator}${toMajor}.${toMinor}`;
}
} else if (
currentVersion &&
npm.isVersion(padZeroes(normalizeVersion(newVersion))) &&
npm.isValid(normalizeVersion(currentValue)) &&
composer2npm(currentValue) === normalizeVersion(currentValue)
) {
newValue = npm.getNewValue({
currentValue: normalizeVersion(currentValue),
rangeStrategy,
currentVersion: normalizeVersion(currentVersion),
newVersion: padZeroes(normalizeVersion(newVersion)),
});
}
if (rangeStrategy === 'widen' && matches(newVersion, currentValue)) {
newValue = currentValue;
} else {
const hasOr = currentValue.includes(' || ');
if (hasOr || rangeStrategy === 'widen') {
const splitValues = currentValue.split('||');
const lastValue = splitValues[splitValues.length - 1];
const replacementValue = getNewValue({
currentValue: lastValue.trim(),
rangeStrategy: 'replace',
currentVersion,
newVersion,
});
if (rangeStrategy === 'replace') {
newValue = replacementValue;
} else if (replacementValue) {
const parsedRange = parseRange(replacementValue);
const element = parsedRange[parsedRange.length - 1];
if (element.operator?.startsWith('<')) {
const splitCurrent = currentValue.split(element.operator);
splitCurrent.pop();
newValue = splitCurrent.join(element.operator) + replacementValue;
} else {
newValue = currentValue + ' || ' + replacementValue;
}
}
}
}
if (!newValue) {
logger.warn(
{ currentValue, rangeStrategy, currentVersion, newVersion },
'Unsupported composer value'
);
newValue = newVersion;
}
if (currentValue.split('.')[0].includes('v')) {
newValue = newValue.replace(regEx(/([0-9])/), 'v$1');
}
// Preserve original min-stability specifier
if (currentValue.includes('@')) {
newValue += '@' + currentValue.split('@')[1];
}
return newValue;
}
function sortVersions(a: string, b: string): number {
return npm.sortVersions(composer2npm(a), composer2npm(b));
}
function isCompatible(version: string): boolean {
return isVersion(version);
}
export const api: VersioningApi = {
equals,
getMajor,
getMinor,
getPatch,
isCompatible,
isGreaterThan,
isLessThanRange,
isSingleVersion,
isStable,
isValid,
isVersion,
matches,
getSatisfyingVersion,
minSatisfyingVersion,
getNewValue,
sortVersions,
};
export default api;