forked from leomotors/anime-captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild.js
51 lines (38 loc) · 1.13 KB
/
prebuild.js
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
// @ts-check
import fs from "node:fs/promises";
const files = (await fs.readdir("./src/data")).filter(
(fname) => fname.endsWith(".json") && !fname.includes(".g.")
);
const categories = [];
const insight = {
categories: {},
created_at: new Date().toISOString(),
};
for (const file of files) {
/** @type {import("./src/data/model").CaptchaTypeJSON} */
const data = JSON.parse((await fs.readFile(`./src/data/${file}`)).toString());
const category = file.replace(".json", "");
categories.push(category);
let correct_count = 0;
data.questions.forEach((question) => {
if (question.answer) correct_count++;
});
insight.categories[category] = {
questions_count: data.questions.length,
correct_count,
correct_ratio:
Math.round((correct_count / data.questions.length) * 1000) / 1000,
};
}
fs.writeFile(
"./src/data/data.g.ts",
`
import type { CaptchaGetAll } from "$data/model";
${categories.reduce(
(prev, cate) => prev + `import * as ${cate} from "./${cate}.json";\n`,
""
)}
export const Data: CaptchaGetAll = { ${categories} };
`
);
fs.writeFile("./src/data/insight.g.json", JSON.stringify(insight));