Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

docs.mjs

64 lines1.7 KB

1import * as Fs from "node:fs"
2import * as Path from "node:path"
3​
4function packages() {
5 return Fs.readdirSync("packages")
6 .filter((_) => Fs.existsSync(Path.join("packages", _, "docs/modules")))
7}
8​
9function pkgName(pkg) {
10 const packageJson = Fs.readFileSync(
11 Path.join("packages", pkg, "package.json")
12 )
13 return JSON.parse(packageJson).name
15​
16function copyFiles(pkg) {
17 const name = pkgName(pkg)
18 const docs = Path.join("packages", pkg, "docs/modules")
19 const dest = Path.join("docs", pkg)
20 const files = Fs.readdirSync(docs, { withFileTypes: true })
21​
22 function handleFiles(root, files) {
23 for (const file of files) {
24 const path = Path.join(docs, root, file.name)
25 const destPath = Path.join(dest, root, file.name)
26​
27 if (file.isDirectory()) {
28 Fs.mkdirSync(destPath, { recursive: true })
29 handleFiles(Path.join(root, file.name), Fs.readdirSync(path, { withFileTypes: true }))
30 continue
31 }
32​
33 const content = Fs.readFileSync(path, "utf8").replace(
34 /^parent: Modules$/m,
35 `parent: "${name}"`
36 )
37 Fs.writeFileSync(destPath, content)
38 }
39 }
40​
41 Fs.rmSync(dest, { recursive: true, force: true })
42 Fs.mkdirSync(dest, { recursive: true })
43 handleFiles("", files)
45​
46function generateIndex(pkg, order) {
47 const name = pkgName(pkg)
48 const content = `---
49title: "${name}"
50has_children: true
51permalink: /docs/${pkg}
52nav_order: ${order}
53---
55​
56 Fs.writeFileSync(Path.join("docs", pkg, "index.md"), content)
58​
59packages().forEach((pkg, i) => {
60 Fs.rmSync(Path.join("docs", pkg), { recursive: true, force: true })
61 Fs.mkdirSync(Path.join("docs", pkg), { recursive: true })
62 copyFiles(pkg)
63 generateIndex(pkg, i + 2)
64})