-
Notifications
You must be signed in to change notification settings - Fork 1
/
eh_translation.ts
72 lines (68 loc) · 1.76 KB
/
eh_translation.ts
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
72
import type { EhDb } from "./db.ts";
import { asyncForEach } from "./utils.ts";
export type GHAuthor = {
name: string;
email: string;
when: string;
};
export type EHTHeader = {
sha: string;
message: string;
author: GHAuthor;
committer: GHAuthor;
};
export type EHTData = {
namespace: string;
frontMatters: {
name: string;
description: string;
key: string;
rules: string[];
};
count: number;
data: {
[x: string]: {
name: string;
intro: string;
links: string;
};
};
};
export type EHTTextFile = {
version: number;
repo: string;
head: EHTHeader;
data: EHTData[];
};
async function fetch_eht_file(signal?: AbortSignal) {
const re = await fetch(
"https://github.com/EhTagTranslation/DatabaseReleases/raw/master/db.text.json",
{ signal },
);
if (re.status != 200) throw Error("fetch failed");
return re.text();
}
export async function load_eht_file(
location: string | undefined = undefined,
signal?: AbortSignal,
): Promise<EHTTextFile> {
const s = location === undefined
? await fetch_eht_file(signal)
: await Deno.readTextFile(location, { signal });
return JSON.parse(s);
}
export async function update_database_tag(
db: EhDb,
f: EHTTextFile,
signal: AbortSignal,
) {
await Deno.writeTextFile("./utt.lock", "", { create: true, signal });
for (const d of f.data) {
await asyncForEach(Object.getOwnPropertyNames(d.data), async (name) => {
const tag = `${d.namespace}:${name}`;
const t = d.data[name];
db.update_tags(tag, t.name, t.intro);
await Deno.readTextFile("./utt.lock", { signal });
});
}
}