47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import * as z from "zod";
|
|
|
|
export const lunarisVersionSchema = z.string().regex(/^\d+(?:\.\d+)+$/u);
|
|
export const catalogFormatVersion = 3;
|
|
|
|
export interface LunarisVersionChange {
|
|
previousVersion: string;
|
|
targetVersion: string;
|
|
}
|
|
|
|
export function parseLunarisVersionChange(
|
|
message: string,
|
|
): LunarisVersionChange | null {
|
|
const match = message.match(
|
|
/^Version Change:\s*(\d+(?:\.\d+)+)\s*→\s*(\d+(?:\.\d+)+)\s*$/imu,
|
|
);
|
|
if (!match) return null;
|
|
return {
|
|
previousVersion: lunarisVersionSchema.parse(match[1]),
|
|
targetVersion: lunarisVersionSchema.parse(match[2]),
|
|
};
|
|
}
|
|
|
|
export function compareLunarisVersions(left: string, right: string): number {
|
|
const leftParts = lunarisVersionSchema.parse(left).split(".").map(Number);
|
|
const rightParts = lunarisVersionSchema.parse(right).split(".").map(Number);
|
|
const length = Math.max(leftParts.length, rightParts.length);
|
|
|
|
for (let index = 0; index < length; index += 1) {
|
|
const difference = (leftParts[index] ?? 0) - (rightParts[index] ?? 0);
|
|
if (difference !== 0) return Math.sign(difference);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
export function catalogSnapshotIsCurrent(
|
|
requestedVersion: string,
|
|
currentVersion: string | null,
|
|
currentFormatVersion: number | null,
|
|
): boolean {
|
|
return Boolean(
|
|
currentVersion &&
|
|
compareLunarisVersions(requestedVersion, currentVersion) <= 0 &&
|
|
currentFormatVersion === catalogFormatVersion
|
|
);
|
|
}
|