File size: 1,190 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { ensureDirSync } from "fs-extra"
import { writeFile } from "node:fs/promises"
import { join } from "node:path"
import { ProjectSdk } from "../utils/project-sdk"

async function main() {
  const project = new ProjectSdk()

  const promises = project.components.map(async (component) => {
    const examples = await project.parseExamples(component)
    const snippet = project.getSnippetCode(component)
    const file = snippet ? project.getSnippetImportPath(component) : undefined
    return { name: component, file, snippet, examples }
  })
  const result = await Promise.all(promises)

  const writePromises = result
    .filter((json) => json.examples.length > 0)
    .map((json) => {
      ensureDirSync(join(project.publicPath, "r", "examples"))
      return writeFile(
        join(project.publicPath, "r", "examples", `${json.name}.json`),
        JSON.stringify(json, null, 2),
      )
    })

  const indexContent = result.flatMap((json) =>
    json.examples.map((ex) => ex.name),
  )
  await Promise.all([
    ...writePromises,
    writeFile(
      join(project.publicPath, "r", "examples", "index.json"),
      JSON.stringify(indexContent, null, 2),
    ),
  ])
}

main()