forked from toeverything/AFFiNE-i18n-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchResources.ts
75 lines (64 loc) · 1.96 KB
/
fetchResources.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
73
74
75
import AdmZip from "adm-zip";
import fs from "node:fs";
import path from "node:path";
const TOLGEE_API_KEY = process.env["TOLGEE_API_KEY"];
const TOLGEE_API_URL = "https://i18n.affine.pro";
const BUILD_DIR = path.resolve(process.cwd(), "build");
if (!TOLGEE_API_KEY) {
throw new Error(`Please set "TOLGEE_API_KEY" as environment variable!`);
}
const withAuth = (fetch: typeof globalThis.fetch): typeof globalThis.fetch => {
const headers = new Headers({
"X-API-Key": TOLGEE_API_KEY,
});
const isRequest = (input: RequestInfo | URL): input is Request => {
return typeof input === "object" && !("href" in input);
};
return new Proxy(fetch, {
apply(
target,
thisArg: unknown,
argArray: Parameters<typeof globalThis.fetch>
) {
if (isRequest(argArray[0])) {
// Request
if (!argArray[0].headers) {
argArray[0] = {
...argArray[0],
headers,
};
}
} else {
// URL or URLLike + ?RequestInit
if (!argArray[1]) {
argArray[1] = {};
}
if (!argArray[1].headers) {
argArray[1].headers = headers;
}
}
return target.apply(thisArg, argArray);
},
});
};
const fetchWithAuth = withAuth(globalThis.fetch);
const downloadTolgeeResources = async (projectName: string) => {
const url = `${TOLGEE_API_URL}/api/project/export/jsonZip`;
const resp = await fetchWithAuth(url);
if (resp.status < 200 || resp.status >= 300) {
throw new Error(url + " " + resp.status + ": " + (await resp.text()));
}
fs.writeFileSync(
path.resolve(BUILD_DIR, `${projectName}.zip`),
Buffer.from(await resp.arrayBuffer())
);
const zip = new AdmZip(path.resolve(BUILD_DIR, `${projectName}.zip`));
zip.extractAllTo(path.resolve(BUILD_DIR, projectName), true);
};
const main = async () => {
if (!fs.existsSync(BUILD_DIR)) {
fs.mkdirSync(BUILD_DIR);
}
await downloadTolgeeResources("AFFiNE-Ligo-Virgo");
};
main();