Skip to content

Commit

Permalink
Use last modified date as "version" with time as the semver tag
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTheMan827 committed Jul 25, 2024
1 parent 633586f commit 7d5c9de
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion shared/generateCoresQmod.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -59,7 +61,10 @@ async function generateCoreQmodJson(version: string): Promise<CoreQmodJSON> {

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({
Expand Down
20 changes: 20 additions & 0 deletions shared/parseUTCDate.ts
Original file line number Diff line number Diff line change
@@ -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]));
}
19 changes: 19 additions & 0 deletions shared/semverDate.ts
Original file line number Diff line number Diff line change
@@ -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("-");
}

0 comments on commit 7d5c9de

Please sign in to comment.