Files
shadcn-ui/apps/v4/scripts/build-examples.ts
shadcn 62aef1117f fix
2026-01-08 21:27:27 +04:00

76 lines
2.0 KiB
TypeScript

import { promises as fs } from "fs"
import path from "path"
import { rimraf } from "rimraf"
const BASES = ["base", "radix"] as const
async function buildExamplesIndex() {
const cwd = process.cwd()
const examplesDir = path.join(cwd, "examples")
console.log("📋 Generating examples/__index__.tsx...")
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 base of BASES) {
const baseDir = path.join(examplesDir, base)
// Check if base directory exists.
try {
await fs.access(baseDir)
} catch {
console.log(` Skipping ${base} - directory does not exist`)
continue
}
// Find all demo files (excluding subdirectories like ui/).
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}`)
index += `
"${base}": {`
for (const file of files) {
const name = file.replace(/\.tsx$/, "")
const filePath = `examples/${base}/${file}`
index += `
"${name}": {
name: "${name}",
filePath: "${filePath}",
component: React.lazy(async () => {
const mod = await import("./${base}/${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 += `
}
`
// Write the index file.
const indexPath = path.join(examplesDir, "__index__.tsx")
rimraf.sync(indexPath)
await fs.writeFile(indexPath, index)
console.log(`\n✅ Generated examples/__index__.tsx`)
}
buildExamplesIndex().catch(console.error)