-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebbuilder.config.js
83 lines (72 loc) · 3.04 KB
/
webbuilder.config.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
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
import path from "node:path"
import fs from "node:fs"
export default {
async build(config, { processPug }) {
globalThis.processPug = processPug
console.log("Generating open graph...")
for await (const f of getFiles("src/pages")) if (f.endsWith(".js")) {
const PageClass = (await import("./" + path.relative(".", f))).default
if (PageClass.description) {
const dir = path.relative("src/pages", path.dirname(f))
writeIndex(dir, PageClass)
}
}
const commands = JSON.parse(fs.readFileSync("src/assets/json/commands.json"))
const queue = [["commands", commands, "commands"]]
while (queue.length) {
const [dir, category, name] = queue.shift()
if (category.categories) queue.push(...Object.entries(category.categories).map(e => [`${dir}/${e[0]}`, e[1], e[0]]))
writeIndex(dir, {
title: `${name.toTitleCase()} - Commands - Wynem`,
description: category.description ? (Array.isArray(category.description) ? category.description[0] : category.description.split("``````")[0]) : `View the commands in the ${name.toTitleCase()} category`
})
if (category.commands) for (const [name, command] of Object.entries(category.commands)) {
writeIndex(path.join(dir, name), {
title: `${name.includes("-") ? name.replace(/-/g, " ").toTitleCase() : name} - Commands - Wynem`,
description: Array.isArray(command.description) ? command.description[0] : command.description.split("``````")[0]
})
}
}
const features = JSON.parse(fs.readFileSync("src/assets/json/features.json"))
for (const f of fs.readdirSync("src/assets/json/features")) {
const data = JSON.parse(fs.readFileSync(`src/assets/json/features/${f}`))
const feature = path.basename(f, ".json")
writeIndex(path.join("features", feature), {
title: `${data.title ?? feature.replace(/-/g, " ").toTitleCase()} - Wynem`,
description: features.find(e => e.id === feature).description
})
}
console.log("Generated open graph")
}
}
globalThis.getFiles = async function*(dir) {
const dirents = await fs.promises.readdir(dir, {withFileTypes: true})
for (const dirent of dirents) {
const res = path.resolve(dir, dirent.name)
if (dirent.isDirectory()) {
yield* getFiles(res)
} else {
yield res
}
}
}
globalThis.Page = class {}
if (!globalThis.navigator) {
globalThis.navigator = {}
}
String.prototype.toTitleCase = function() {
return this.replace(/\w\S*/g, t => t.charAt(0).toUpperCase() + t.substring(1).toLowerCase()).trim()
}
function writeIndex(dir, args) {
fs.mkdirSync(path.join("dist", dir), { recursive: true })
fs.writeFileSync(path.join("dist", dir, "index.html"), processPug(`extends /../includes/main.pug
block meta
-
meta = {
title: "${args.title}",
description: "${args.description.replace(/\n/g, " ").replace(/"/g, '\\"')}",
${args.image ? `image: "${args.image}",` : ""}
${args.colour ? `colour: "${args.colour}",` : ""}
${args.icon ? `icon: "${args.icon}"` : ""}
}`))
}