-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenReadme.js
More file actions
159 lines (151 loc) · 3.72 KB
/
genReadme.js
File metadata and controls
159 lines (151 loc) · 3.72 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fs = require("fs");
const path = require("path");
let head =
"# CAHBotpacks\n\
packs For Card Against Humanity Bot in Telegram\n\
## English Packs\n\
Packs used on english language\n\
### Packs\n";
/**
*
* @param {any} header
* @param {Array<any>} data
* @param {Array<string>} order
* @returns
*/
function tableBuilder(header, data, order, withTotal = "pack") {
let out = "";
order.forEach((v) => {
out += "| " + header[v] + " ";
});
out += "|\n";
order.forEach((v) => {
out += "|---";
});
out += "|\n";
data.forEach((v) => {
order.forEach((v2) => {
out += "| " + v[v2] + " ";
});
out += "|\n";
});
switch (withTotal) {
case "pack":
out += `\n|Total pack amount: ${
data.length
}|Total questions amount: ${data.reduce(
(out, now) => out + now.qcount,
0
)}|Total answer amount: ${data.reduce(
(out, now) => out + now.acount,
0
)}|\n|---|---|---|\n`;
break;
case "amount":
out += `\n|Total amount: ${data.length}|\n|---|`;
break;
default:
}
return out;
}
/**
* @type {any[]}
*/
let enPack = JSON.parse(
fs.readFileSync(path.join(__dirname, "en", "packs.json"))
);
enPack = enPack
.sort((a, b) => {
if (a.name === b.name) return 0;
return a.name < b.name ? -1 : 1;
})
.sort((a, b) => {
let order = ["official", "fun-based"];
return order.indexOf(a.type) - order.indexOf(b.type);
});
head +=
"\n" +
tableBuilder(
{
name: "Name of pack",
type: "Type of pack",
qcount: "Questions count",
acount: "Answers count",
},
enPack.map((el) => {
return { ...el, name: `[${el.name}](${el.pathTo})` };
}),
["name", "type", "qcount", "acount"]
);
let packhead =
"## {name}\nKey: `{key}` \nType: {type} \nQuestion amount: {quest} \nAnswers amount: {ans}\n### Questions\n{tquest}\n\n### Answers\n{tans}";
let ffn = fs.readdirSync(path.join(__dirname, "en"), {
withFileTypes: true,
});
ffn.forEach((el) => {
if (el.isDirectory()) {
let pack = JSON.parse(
fs.readFileSync(path.join(__dirname, "en", el.name, "pack.json"))
);
/**
* @type {any[]}
*/
let packa = JSON.parse(
fs.readFileSync(path.join(__dirname, "en", el.name, "answers.json"))
);
/**
* @type {any[]}
*/
let packq = JSON.parse(
fs.readFileSync(path.join(__dirname, "en", el.name, "questions.json"))
);
fs.writeFileSync(
path.join(__dirname, "en", el.name, "README.md"),
packhead
.replace("{name}", pack.name)
.replace("{key}", pack.key)
.replace("{type}", pack.type)
.replace("{quest}", pack.qcount)
.replace("{ans}", pack.acount)
.replace(
"{tquest}",
tableBuilder(
{ text: "Question", pick: "Amount of answers" },
packq.map((el) => {
return { ...el, text: el.text.replace(/\n/g, " ") };
}),
["text", "pick"],
"amount"
)
)
.replace(
"{tans}",
tableBuilder(
{ text: "Answers" },
packa.map((el) => {
return { ...el, text: el.text.replace(/[\\n\\r]+/g, " ") };
}),
["text"],
"amount"
)
)
);
}
});
fs.writeFileSync(
path.join(__dirname, "en", "README.md"),
"### Packs\n" +
tableBuilder(
{
name: "Name of pack",
type: "Type of pack",
qcount: "Questions count",
acount: "Answers count",
},
enPack.map((el) => {
return { ...el, name: `[${el.name}](${path.basename(el.pathTo)})` };
}),
["name", "type", "qcount", "acount"]
)
);
fs.writeFileSync("README.md", head);