Add Prettier and ESLint tooling (Refs #77)
This commit is contained in:
+34
-10
@@ -52,9 +52,16 @@ export const PLUTONIUM_MANIFEST_URL = 'https://cdn.plutoniummod.com/updater/prod
|
||||
|
||||
export function parsePlutoniumManifest(value: unknown): number {
|
||||
const manifest = value as { product?: unknown; revision?: unknown } | null;
|
||||
if (!manifest || manifest.product !== 'plutonium-core-prod' || typeof manifest.revision !== 'number'
|
||||
|| !Number.isSafeInteger(manifest.revision) || manifest.revision < 1) {
|
||||
throw new Error('The production updater manifest did not contain a valid Plutonium revision.');
|
||||
if (
|
||||
!manifest ||
|
||||
manifest.product !== 'plutonium-core-prod' ||
|
||||
typeof manifest.revision !== 'number' ||
|
||||
!Number.isSafeInteger(manifest.revision) ||
|
||||
manifest.revision < 1
|
||||
) {
|
||||
throw new Error(
|
||||
'The production updater manifest did not contain a valid Plutonium revision.'
|
||||
);
|
||||
}
|
||||
return manifest.revision;
|
||||
}
|
||||
@@ -66,12 +73,20 @@ export function nextMaintenanceAt(time: string, now = new Date(), timeZone = 'UT
|
||||
const [hour, minute] = time.split(':').map(Number);
|
||||
const target = hour * 60 + minute;
|
||||
const formatter = new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone, year: 'numeric', month: '2-digit', day: '2-digit',
|
||||
hour: '2-digit', minute: '2-digit', hourCycle: 'h23'
|
||||
timeZone,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hourCycle: 'h23'
|
||||
});
|
||||
function local(at: number) {
|
||||
const parts = Object.fromEntries(formatter.formatToParts(at).map(p => [p.type, p.value]));
|
||||
return { day: `${parts.year}-${parts.month}-${parts.day}`, minute: Number(parts.hour) * 60 + Number(parts.minute) };
|
||||
const parts = Object.fromEntries(formatter.formatToParts(at).map((p) => [p.type, p.value]));
|
||||
return {
|
||||
day: `${parts.year}-${parts.month}-${parts.day}`,
|
||||
minute: Number(parts.hour) * 60 + Number(parts.minute)
|
||||
};
|
||||
}
|
||||
const today = local(now.getTime()).day;
|
||||
const start = Math.floor(now.getTime() / 60_000) * 60_000;
|
||||
@@ -83,9 +98,13 @@ export function nextMaintenanceAt(time: string, now = new Date(), timeZone = 'UT
|
||||
const wall = local(at);
|
||||
if (wall.day < today || wall.minute < target) continue;
|
||||
const previous = days.get(wall.day);
|
||||
if (!previous || wall.minute < previous.minute) days.set(wall.day, { at, minute: wall.minute });
|
||||
if (!previous || wall.minute < previous.minute)
|
||||
days.set(wall.day, { at, minute: wall.minute });
|
||||
}
|
||||
const next = [...days.values()].map(d => d.at).filter(at => at > now.getTime()).sort((a, b) => a - b)[0];
|
||||
const next = [...days.values()]
|
||||
.map((d) => d.at)
|
||||
.filter((at) => at > now.getTime())
|
||||
.sort((a, b) => a - b)[0];
|
||||
if (next === undefined) throw new Error('Could not calculate the next maintenance time.');
|
||||
return new Date(next).toISOString();
|
||||
}
|
||||
@@ -94,5 +113,10 @@ export function nextMaintenanceAt(time: string, now = new Date(), timeZone = 'UT
|
||||
export function maintenanceTimeInZone(schedule: MaintenanceSchedule, timeZone: string): string {
|
||||
if (schedule.timeZone === timeZone) return schedule.time;
|
||||
const at = schedule.nextAt ?? nextMaintenanceAt(schedule.time, new Date(), schedule.timeZone);
|
||||
return new Intl.DateTimeFormat('en-GB', { timeZone, hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }).format(new Date(at));
|
||||
return new Intl.DateTimeFormat('en-GB', {
|
||||
timeZone,
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hourCycle: 'h23'
|
||||
}).format(new Date(at));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user