Files
renovate/lib/modules/versioning/distro.ts
T
Sebastian Poxhofer e0cec3dc95 refactor(lint): add types-location rule and enable it for lib/modules (#45497)
feat(lint): add `types-location` rule and enable it for `lib/modules`

Reviewers consistently ask for exported types to be moved into a
colocated `types.ts`. Add a `renovate/types-location` oxlint rule that
reports `export interface` / `export type` declarations outside such a
file, and turn it on across `lib/modules`.

Re-exports (`export type { X }`, `export type *`), file-local types, and
values (enums, classes, functions) are left alone. `schema.ts` files and
`schema/` directories are excluded via config, matching how
`zod-schema-location` is configured — inferred types belong next to the
schema they are derived from.

Relocating the 87 existing violations means:

- new `types.ts` files for the modules that had none
- `terragrunt/common.ts` folded into its `types.ts`, as it held nothing
  else
- `RangeBound` in `versioning/gradle/compare.ts` un-exported, since only
  a file-local interface referenced it
- `bazel-module/parser/fragments.ts` split from its inferred types, which
  now live in `parser/types.ts` and infer through a namespace import so
  each schema keeps its merged name
2026-08-27 19:52:09 +00:00

197 lines
5.2 KiB
TypeScript

import { DateTime } from 'luxon';
import dataFiles from '../../data-files.generated.ts';
import { regEx } from '../../util/regex.ts';
import type {
DistroDataFile,
DistroInfoRecord,
DistroInfoRecordWithVersion,
DistroSchedule,
} from './types.ts';
// Days to delay new releases
const delay = 1;
export class DistroInfo {
private readonly _codenameToVersion = new Map<
string,
DistroInfoRecordWithVersion
>();
private readonly _sortedInfo: DistroInfoRecordWithVersion[] = [];
private readonly _distroInfo: DistroInfoRecord;
constructor(distroJsonKey: DistroDataFile) {
this._distroInfo = JSON.parse(
dataFiles.get(distroJsonKey)!.replace(regEx(/v([\d.]+)\b/gm), '$1'),
);
for (const version of Object.keys(this._distroInfo)) {
const schedule = this._distroInfo[version];
this._codenameToVersion.set(schedule.series, { version, ...schedule });
}
const arr = Object.keys(this._distroInfo).sort(
(a, b) => parseFloat(a) - parseFloat(b),
);
for (const v of arr) {
const obj = { version: v, ...this._distroInfo[v.toString()] };
if (!obj.release) {
// istanbul ignore next
continue;
}
this._sortedInfo.push(obj);
}
}
/**
* Check if input is a valid release codename
* @param input A codename
* @returns true if input is a codename, false otherwise
*/
public isCodename(input: string): boolean {
return this._codenameToVersion.has(input);
}
/**
* Checks if given input string is a valid release version
* @param input A codename/semVer
* @returns true if release exists, false otherwise
*/
public exists(input: string): boolean {
const ver = this.getVersionByCodename(input);
return !!this._distroInfo[ver];
}
/**
* Get semVer representation of a given codename
* @param input A codename
* @returns A semVer if exists, otherwise input string is returned
*/
public getVersionByCodename(input: string): string {
const schedule = this._codenameToVersion.get(input);
if (schedule) {
return schedule.version;
}
return input;
}
/**
* Get codename representation of a given semVer
* @param input A semVer
* @returns A codename if exists, otherwise input string is returned
*/
public getCodenameByVersion(input: string): string {
const di = this._distroInfo[input];
if (di) {
return di.series;
}
// istanbul ignore next
return input;
}
/**
* Get schedule of a given release
* @param input A codename/semVer
* @returns A schedule if available, otherwise undefined
*/
public getSchedule(input: string): DistroSchedule | null {
const ver = this.getVersionByCodename(input);
return this._distroInfo[ver] ?? null;
}
/**
* Check if a given release has passed its EOL
* @param input A codename/semVer
* @returns false if still supported, true otherwise
*/
public isEolLts(input: string): boolean {
const ver = this.getVersionByCodename(input);
const schedule = this.getSchedule(ver);
const endLts = schedule?.eol ?? null;
let end = schedule?.eol_lts ?? null;
// ubuntu: does not have eol_lts
// debian: only "Stable" has no eol_lts, old and oldold has both
end ??= endLts;
if (end) {
const now = DateTime.now().toUTC();
const eol = DateTime.fromISO(end, { zone: 'utc' });
return eol < now;
}
return true;
}
/**
* Check if a given version has been released
* @param input A codename/semVer
* @returns false if unreleased or has no schedule, true otherwise
*/
public isReleased(input: string): boolean {
const ver = this.getVersionByCodename(input);
const schedule = this.getSchedule(ver);
if (!schedule?.release) {
return false;
}
const now = DateTime.now().minus({ day: delay }).toUTC();
const release = DateTime.fromISO(schedule.release, { zone: 'utc' });
return release < now;
}
/**
* Check if a given version has been created
* @param input A codename/semVer
* @returns false if unreleased or has no schedule, true otherwise
*/
public isCreated(input: string): boolean {
const ver = this.getVersionByCodename(input);
const schedule = this.getSchedule(ver);
if (!schedule) {
return false;
}
const now = DateTime.now().minus({ day: delay }).toUTC();
const created = DateTime.fromISO(schedule.created, { zone: 'utc' });
return created < now;
}
/**
* Get distro info for the release that has N other newer releases.
* Example: n=0 corresponds to the latest available release, n=1 the release before, etc.
* In Debian terms: N = 0 -> stable, N = 1 -> oldstable, N = 2 -> oldoldstalbe
* @param n
* @returns Distro info of the Nth latest release
*/
public getNLatest(n: number): DistroInfoRecordWithVersion | null {
const len = this._sortedInfo.length - 1;
let idx = -1;
if (n < 0) {
return null;
}
for (let i = len; i >= 0; i--) {
if (this.isReleased(this._sortedInfo[i].version)) {
// 'i' holds the latest released version index
// compensate for the requested 'n'
idx = i - Math.floor(n);
break;
}
}
if (idx > len || idx < 0) {
return null;
}
return this._sortedInfo[idx];
}
}