0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 21:48:32 +00:00
renovatebot_renovate/lib/modules/versioning/devbox/index.ts
Will Brennan 01a3c2ff8f
feat(versioning): add devbox versioning module (#31408)
Co-authored-by: Rhys Arkins <rhys@arkins.net>
2024-12-20 15:05:23 +00:00

75 lines
2.2 KiB
TypeScript

import { regEx } from '../../../util/regex';
import type { GenericVersion } from '../generic';
import { GenericVersioningApi } from '../generic';
import type { VersioningApi } from '../types';
export const id = 'devbox';
export const displayName = 'devbox';
export const urls = [];
export const supportsRanges = false;
const validPattern = regEx(/^((\d|[1-9]\d*)(\.(\d|[1-9]\d*)){0,2})$/);
const versionPattern = regEx(/^((\d|[1-9]\d*)(\.(\d|[1-9]\d*)){2})$/);
class DevboxVersioningApi extends GenericVersioningApi {
protected _parse(version: string): GenericVersion | null {
const matches = validPattern.exec(version);
if (!matches) {
return null;
}
const release = matches[0].split('.').map(Number);
return { release };
}
override isValid(version: string): boolean {
if (version === 'latest') {
return true;
}
return this._parse(version) !== null;
}
override isVersion(version: string): boolean {
if (version === 'latest') {
return false;
}
const matches = versionPattern.exec(version);
return !!matches;
}
override matches(version: string, range: string): boolean {
return this.isVersion(version) && this.equals(version, range);
}
protected override _compare(version: string, other: string): number {
const parsed1 = this._parse(version);
const parsed2 = this._parse(other);
// Treat "latest" as * and always return equal
if (other === 'latest' && parsed1) {
return 0;
}
// If either version is invalid, return unequal
if (!(parsed1 && parsed2)) {
return 1;
}
// support variable length compare
const length = Math.max(parsed1.release.length, parsed2.release.length);
for (let i = 0; i < length; i += 1) {
// 2.1 and 2.1.0 are equivalent
const part1 = parsed1.release[i];
const part2 = parsed2.release[i];
// if part1 or part2 is undefined, we should treat them as equal
// e.g. 1.0.0 === 1.0
if (part1 !== undefined && part2 !== undefined && part1 !== part2) {
return part1 - part2;
}
}
return 0;
}
}
export const api: VersioningApi = new DevboxVersioningApi();
export default api;