diff --git a/shared/generateCoresQmod.ts b/shared/generateCoresQmod.ts index 636e5c89..41316785 100644 --- a/shared/generateCoresQmod.ts +++ b/shared/generateCoresQmod.ts @@ -1,6 +1,8 @@ import JSZip from "jszip"; import { getCoreMods } from "./CoreMods"; import { ModLoader } from "./types/ModLoader"; +import { semverDate } from "./semverDate"; +import { parseUTCDate } from "./parseUTCDate"; /** * Interface representing the structure of the CoreQmodJSON object. @@ -59,7 +61,10 @@ async function generateCoreQmodJson(version: string): Promise { if (cores[version]) { const lastUpdated = cores[version].lastUpdated; - json.version = `1.0.0-${lastUpdated.replace(/[^0-9TZ-]+/g, '-')}` + + try { + json.version = semverDate(parseUTCDate(lastUpdated)); + } catch (_: any) { } for (const mod of cores[version].mods) { json.dependencies.push({ diff --git a/shared/parseUTCDate.ts b/shared/parseUTCDate.ts new file mode 100644 index 00000000..5e6cc2f4 --- /dev/null +++ b/shared/parseUTCDate.ts @@ -0,0 +1,20 @@ +/** + * Parses a date string in UTC format and returns a Date object. + * + * @param dateString - The date string to parse in the format "YYYY-MM-DDTHH:mm:ss.sssZ". + * @returns - The parsed Date object in UTC. + */ +export function parseUTCDate(dateString: string): Date { + const dateMatch = dateString + .match(/^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(?:\.(\d{1,3}))?\d*Z$/) + + if (!dateMatch) { + throw new Error("Invalid date format"); + } + + const dateComponents = dateMatch + .slice(1) + .map((value) => value ? parseInt(value) : 0); + + return new Date(Date.UTC(dateComponents[0], dateComponents[1] - 1, dateComponents[2], dateComponents[3], dateComponents[4], dateComponents[5], dateComponents[6])); +} diff --git a/shared/semverDate.ts b/shared/semverDate.ts new file mode 100644 index 00000000..1a31f28b --- /dev/null +++ b/shared/semverDate.ts @@ -0,0 +1,19 @@ +/** + * Converts a Date object to a semver-like string. + * If no date is provided, the current date and time is used. + * + * @param inputDate - The date to be converted. Defaults to the current date and time. + * @returns The date and time in the format "YYYY.MM.DD-HHmmssSSSZ". + */ +export function semverDate(inputDate: Date = new Date()): string { + const isoString = inputDate.toISOString(); + const parts = isoString.split("T").map((value, index) => { + if (index == 0) { + return value.replace(/-0*/g, "."); + } else { + return value.replace(/[:\.]/g, ""); + } + }); + + return parts.join("-"); +}