-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcheck.ts
More file actions
71 lines (60 loc) · 1.7 KB
/
check.ts
File metadata and controls
71 lines (60 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Deno script to ensure the core mods are valid
interface CoreModListing {
[version: string]: GameCoreMods;
}
interface GameCoreMods {
lastUpdated: string;
mods: CoreMod[];
}
interface CoreMod {
id: string;
version: string;
downloadLink: string;
}
function isValidTime(timeStr: string) {
// Use Date parsing to check for valid ISO 8601 timestamp
const d = new Date(timeStr);
return !isNaN(d.getTime());
}
function isValidSemver(version: string) {
// assert string is semver
return /^\d+\.\d+\.\d+$/.test(version);
}
async function isValidUrl(url: string) {
// assert string is a valid URL
try {
const res = await fetch(url, { method: "HEAD" });
if (!res.ok) return false;
return true;
} catch {
return false;
}
}
const json: CoreModListing = JSON.parse(
await Deno.readTextFile("./core_mods.json")
);
delete json["$schema"]; // Remove schema reference if present
// ensure all have valid time
Object.entries(json).forEach(([version, coreMods]) => {
console.log(`Checking version ${version}`);
if (!isValidTime(coreMods.lastUpdated)) {
throw new Error(
`Invalid lastUpdated time for version ${version}: ${coreMods.lastUpdated}`
);
}
coreMods.mods.forEach(async (mod) => {
if (!isValidSemver(mod.version)) {
throw new Error(
`Invalid version for mod ${mod.id} in version ${version}: ${mod.version}`
);
}
const validURL = await isValidUrl(mod.downloadLink);
if (!validURL) {
throw new Error(
`Invalid download link for mod ${mod.id} in version ${version}: ${mod.downloadLink}`
);
}
// Green color for valid mods using "colorette"
console.log(`%cMod ${mod.id} is valid`, "color: green");
});
});