1 import { slugChanges } from "../generated/slug-changes.mjs"
2 import { readdirSync, readFileSync, statSync, writeFileSync } from "fs"
3 import path from "path"
4
5 const baseAppPath = path.resolve("app")
6 const baseReferencePath = path.resolve("references")
7 const monoRepoPath = path.resolve("..", "..", "..")
8
9 /**
10 * Scans the `app/` directory for `page.mdx` content files.
11 *
12 * @param {object} options
13 * @param {string} [options.dir] - The directory to scan
14 * @param {string} [options.basePath] - The path to consider as base
15 * @param {string} [options.baseSlug] - The path to derive the pathname from
16 * @returns {Promise<{ filePath: string; pathname: string; }[]>}
17 */
18 async function scanAppFiles(options = {}) {
19 const { dir = "", basePath = baseAppPath, baseSlug = baseAppPath } = options
20 const filesMap = []
21 const fullPath = path.resolve(basePath, dir.replace(/^\//, ""))
22 const files = readdirSync(fullPath)
23
24 for (const file of files) {
25 const filePath = path.join(fullPath, file)
26 const fileBasename = path.basename(file)
27 if (fileBasename !== "page.mdx") {
28 if (statSync(filePath).isDirectory()) {
29 filesMap.push(
30 ...(await scanAppFiles({
31 dir: filePath.replace(basePath, ""),
32 basePath,
33 baseSlug,
34 }))
35 )
36 }
37 continue
38 }
39
40 const slugChange = slugChanges.find(
41 (item) => item.origSlug === filePath.replace(basePath, "")
42 )
43
44 const pathname =
45 slugChange?.newSlug ||
46 filePath.replace(baseSlug, "").replace(`/${fileBasename}`, "")
47
48 filesMap.push({
49 filePath: filePath.replace(monoRepoPath, ""),
50 pathname: pathname.length ? pathname : "/",
51 })
52 }
53
54 return filesMap
55 }
56
57 /**
58 * Scans the `references/` directory for `page.json` doc-model files. Each entry
59 * is keyed by the DocPage's baked-in `slug` (its final URL), so the reference
60 * route resolves pages — including slug-overridden ones — with a direct lookup.
61 *
62 * @param {string} [dir] - The directory to scan (defaults to references/)
63 * @returns {{ filePath: string; pathname: string; }[]}
64 */
65 function scanReferenceFiles(dir = baseReferencePath) {
66 const filesMap = []
67 let files
68 try {
69 files = readdirSync(dir)
70 } catch {
71 return filesMap
72 }
73
74 for (const file of files) {
75 const filePath = path.join(dir, file)
76 if (path.basename(file) !== "page.json") {
77 if (statSync(filePath).isDirectory()) {
78 filesMap.push(...scanReferenceFiles(filePath))
79 }
80 continue
81 }
82
83 let pathname = filePath.replace(path.resolve(), "").replace("/page.json", "")
84 try {
85 const docPage = JSON.parse(readFileSync(filePath, "utf-8"))
86 if (docPage.slug) {
87 pathname = docPage.slug
88 }
89 } catch {
90 // keep the path-derived pathname on parse failure
91 }
92
93 filesMap.push({
94 filePath: filePath.replace(monoRepoPath, ""),
95 pathname,
96 })
97 }
98
99 return filesMap
100 }
101
102 export async function main() {
103 const filesMap = [...(await scanAppFiles()), ...scanReferenceFiles()]
104
105 // write files map
106 writeFileSync(
107 path.resolve("generated", "files-map.mjs"),
108 `export const filesMap = ${JSON.stringify(filesMap, null, 2)}`
109 )
110 }