-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (63 loc) · 2.48 KB
/
index.js
File metadata and controls
77 lines (63 loc) · 2.48 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
import fs from "fs";
import path from "path";
const pathPrefix = "developer/commands";
const outputDir = path.resolve(`./${pathPrefix}`);
const getCommands = async () => {
return await fetch(
"https://raw.githubusercontent.com/clashperk/clashperk/refs/heads/main/scripts/commands_export.json"
).then((res) => res.json());
};
(async () => {
const commands = await getCommands();
// fs.rmSync(outputDir, { recursive: true, force: true });
// fs.mkdirSync(outputDir, { recursive: true });
const toFileName = (name) =>
name
.replace(/\s+/g, "-")
.replace(/[^\w\-]/g, "")
.toLowerCase();
let summary = `* [Commands](${pathPrefix}/README.md)\n`;
commands.sort((a, b) => a.name.localeCompare(b.name));
for (const cmd of commands) {
const parts = cmd.name.split(" ");
const [parent, ...rest] = parts;
const sub = rest.join(" ");
const parentDir = path.join(outputDir, toFileName(parent));
const filePath = sub
? path.join(parentDir, `${toFileName(sub)}.md`)
: path.join(outputDir, `${toFileName(parent)}.md`);
if (sub) fs.mkdirSync(parentDir, { recursive: true });
let md = `---\ndescription: ${cmd.description}\n---\n\n`;
md += `# ${cmd.name}\n\n`;
md += `${cmd.description_long || cmd.description}\n\n`;
if (cmd.options && cmd.options.length > 0) {
md += `## Options\n\n`;
md += `| Name | Description |\n`;
md += `|------|-------------|\n`;
for (const opt of cmd.options) {
const isBoolean = opt.choices.every((item) => ['Yes', 'No'].includes(item))
const choices =
Array.isArray(opt.choices) && opt.choices.length && !isBoolean
? `<details><summary>View Options</summary>${opt.choices.map((c) => `\`${c}\``).join(", ")}</details>`
: "";
const desc = opt.description;
md += `| \`${opt.name}\` | ${desc}${choices} |\n`;
}
md += `\n`;
}
fs.writeFileSync(filePath, md, "utf8");
if (!sub) {
summary += ` * [${parent}](${pathPrefix}/${toFileName(parent)}.md)\n`;
} else {
const parentSection = ` * [${parent}](${pathPrefix}/${toFileName(
parent
)}/README.md)\n`;
if (!summary.includes(parentSection)) summary += parentSection;
summary += ` * [${cmd.name}](${pathPrefix}/${toFileName(
parent
)}/${toFileName(sub)}.md)\n`;
}
}
fs.writeFileSync(path.join(outputDir, "SUMMARY.md"), summary, "utf8");
console.log("✅ GitBook documentation generated.");
})();