1 /* eslint-disable no-console */
2 import { readdirSync, writeFileSync } from "fs"
3 import path from "path"
4 import { fileURLToPath } from "url"
5
6 const __dirname = path.dirname(fileURLToPath(import.meta.url))
7 const specsDir = path.resolve(__dirname, "../specs/components")
8 const outputPath = path.resolve(__dirname, "../generated/components-index.mjs")
9
10 export function generateSpecsIndex() {
11 const componentDirs = readdirSync(specsDir, { withFileTypes: true }).filter(
12 (entry) => entry.isDirectory()
13 )
14
15 /** @type {Record<string, string[]>} */
16 const index = {}
17
18 for (const dir of componentDirs) {
19 const files = readdirSync(path.join(specsDir, dir.name)).filter((f) =>
20 f.endsWith(".json")
21 )
22 index[dir.name] = files
23 }
24
25 const entries = Object.entries(index)
26 .map(([component, files]) => {
27 const filesStr = files.map((f) => `"${f}"`).join(", ")
28 return ` "${component}": [${filesStr}]`
29 })
30 .join(",\n")
31
32 const content = `/**
33 * @type {Record<string, string[]>}
34 */
35 export const ComponentSpecsIndex = {
36 ${entries},
37 }
38 `
39
40 writeFileSync(outputPath, content)
41 console.log(`Generated specs index at ${outputPath}`)
42 }