-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-data.mts
62 lines (54 loc) · 1.55 KB
/
update-data.mts
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
import fs from "fs/promises";
import { gql, GraphQLClient } from "graphql-request";
const client = new GraphQLClient("https://data.loathers.net/graphql");
type JobType = {
entity: string;
disambiguate: boolean;
};
const jobs: JobType[] = [
{ entity: "classes", disambiguate: false },
{ entity: "effects", disambiguate: true },
{ entity: "familiars", disambiguate: false },
{ entity: "items", disambiguate: true },
{ entity: "locations", disambiguate: false },
{ entity: "monsters", disambiguate: true },
{ entity: "paths", disambiguate: false },
{ entity: "skills", disambiguate: true },
];
const titleCase = (i: string) => i.slice(0, 1).toUpperCase() + i.slice(1);
async function exists(file: string) {
try {
await fs.access(file);
return true;
} catch {
return false;
}
}
async function main() {
if (!(await exists("data"))) await fs.mkdir("data");
await Promise.all(
jobs.map(async ({ entity, disambiguate }) => {
const group = `all${titleCase(entity)}`;
const data = await client.request<{
[group: string]: {
nodes: { id: number; name: string; ambiguous?: boolean }[];
};
}>(gql`
{
${group} {
nodes {
id
name
${disambiguate ? "ambiguous" : ""}
}
}
}
`);
const names = data[group].nodes.map((node) =>
node.ambiguous ? `[${node.id}]${node.name}` : node.name,
);
return await fs.writeFile(`data/${entity}.json`, JSON.stringify(names));
}),
);
}
main();