mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-02-09 02:49:29 +08:00
* feat: rtl * feat * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * feat: add sidebar * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * chore: changeset * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
import { promises as fs } from "fs"
|
|
import path from "path"
|
|
import { rimraf } from "rimraf"
|
|
|
|
import { BASES } from "@/registry/bases"
|
|
|
|
async function buildExamplesIndex() {
|
|
const cwd = process.cwd()
|
|
const examplesDir = path.join(cwd, "examples")
|
|
|
|
console.log("📋 Generating examples/__index__.tsx...")
|
|
|
|
// Process all bases in parallel.
|
|
const baseResults = await Promise.all(
|
|
Array.from(BASES).map(async (base) => {
|
|
const baseDir = path.join(examplesDir, base.name)
|
|
|
|
try {
|
|
await fs.access(baseDir)
|
|
} catch {
|
|
console.log(` Skipping ${base.name} - directory does not exist`)
|
|
return null
|
|
}
|
|
|
|
const allEntries = await fs.readdir(baseDir, { withFileTypes: true })
|
|
const files = allEntries
|
|
.filter((entry) => entry.isFile() && entry.name.endsWith(".tsx"))
|
|
.map((entry) => entry.name)
|
|
.sort()
|
|
|
|
console.log(` Found ${files.length} demos for ${base.name}`)
|
|
|
|
return { base, files }
|
|
})
|
|
)
|
|
|
|
let index = `// @ts-nocheck
|
|
// This file is autogenerated by scripts/build-examples-index.mts
|
|
// Do not edit this file directly.
|
|
import * as React from "react"
|
|
|
|
export const ExamplesIndex: Record<string, Record<string, any>> = {`
|
|
|
|
for (const result of baseResults) {
|
|
if (!result) continue
|
|
|
|
const { base, files } = result
|
|
|
|
index += `
|
|
"${base.name}": {`
|
|
|
|
for (const file of files) {
|
|
const name = file.replace(/\.tsx$/, "")
|
|
|
|
index += `
|
|
"${name}": {
|
|
name: "${name}",
|
|
filePath: "examples/${base.name}/${file}",
|
|
component: React.lazy(async () => {
|
|
const mod = await import("./${base.name}/${name}")
|
|
const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') || "${name}"
|
|
return { default: mod.default || mod[exportName] }
|
|
}),
|
|
},`
|
|
}
|
|
|
|
index += `
|
|
},`
|
|
}
|
|
|
|
index += `
|
|
}
|
|
`
|
|
|
|
const indexPath = path.join(examplesDir, "__index__.tsx")
|
|
await rimraf(indexPath)
|
|
await fs.writeFile(indexPath, index)
|
|
|
|
console.log(`\n✅ Generated examples/__index__.tsx`)
|
|
}
|
|
|
|
buildExamplesIndex().catch(console.error)
|