|
| 1 | +/** |
| 2 | + * Build static API responses from the registry data. |
| 3 | + * |
| 4 | + * Generates /public/api/registry/ files that work on CF Pages, |
| 5 | + * GitHub Pages, or any static hosting. Run after build-registry.mts. |
| 6 | + * |
| 7 | + * Output: |
| 8 | + * public/api/registry/index.json — full manifest (all components + source) |
| 9 | + * public/api/registry/components.json — component list (no source) |
| 10 | + * public/api/registry/components/{name}.json — individual component with source |
| 11 | + * public/api/registry/search-index.json — lightweight search index |
| 12 | + */ |
| 13 | + |
| 14 | +import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "fs" |
| 15 | +import path from "path" |
| 16 | + |
| 17 | +const REGISTRY_DIR = path.join(process.cwd(), "public/registry") |
| 18 | +const STYLES_DIR = path.join(REGISTRY_DIR, "styles/default") |
| 19 | +const API_DIR = path.join(process.cwd(), "public/api/registry") |
| 20 | +const COMPONENTS_DIR = path.join(API_DIR, "components") |
| 21 | + |
| 22 | +interface RegistryItem { |
| 23 | + name: string |
| 24 | + type: string |
| 25 | + dependencies?: string[] |
| 26 | + devDependencies?: string[] |
| 27 | + registryDependencies?: string[] |
| 28 | + files: Array<{ name: string; content: string } | string> |
| 29 | + description?: string |
| 30 | + category?: string |
| 31 | +} |
| 32 | + |
| 33 | +function main() { |
| 34 | + console.log("Building static registry API files...") |
| 35 | + |
| 36 | + // Read the index |
| 37 | + const indexPath = path.join(REGISTRY_DIR, "index.json") |
| 38 | + if (!existsSync(indexPath)) { |
| 39 | + console.error("Registry index.json not found. Run registry:build first.") |
| 40 | + process.exit(1) |
| 41 | + } |
| 42 | + |
| 43 | + const index: RegistryItem[] = JSON.parse(readFileSync(indexPath, "utf-8")) |
| 44 | + |
| 45 | + // Read all component files |
| 46 | + const components = new Map<string, RegistryItem>() |
| 47 | + if (existsSync(STYLES_DIR)) { |
| 48 | + const files = readdirSync(STYLES_DIR).filter((f) => f.endsWith(".json")) |
| 49 | + for (const file of files) { |
| 50 | + try { |
| 51 | + const data: RegistryItem = JSON.parse( |
| 52 | + readFileSync(path.join(STYLES_DIR, file), "utf-8") |
| 53 | + ) |
| 54 | + components.set(data.name, data) |
| 55 | + } catch { |
| 56 | + // skip |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Create output directories |
| 62 | + mkdirSync(COMPONENTS_DIR, { recursive: true }) |
| 63 | + |
| 64 | + // 1. Component list (no source — lightweight) |
| 65 | + const componentList = index.map((item) => ({ |
| 66 | + name: item.name, |
| 67 | + type: item.type, |
| 68 | + dependencies: item.dependencies, |
| 69 | + registryDependencies: item.registryDependencies, |
| 70 | + })) |
| 71 | + |
| 72 | + writeFileSync( |
| 73 | + path.join(API_DIR, "components.json"), |
| 74 | + JSON.stringify({ total: componentList.length, components: componentList }) |
| 75 | + ) |
| 76 | + console.log(` components.json: ${componentList.length} components`) |
| 77 | + |
| 78 | + // 2. Individual component files (with source) |
| 79 | + let written = 0 |
| 80 | + for (const [name, data] of components) { |
| 81 | + writeFileSync( |
| 82 | + path.join(COMPONENTS_DIR, `${name}.json`), |
| 83 | + JSON.stringify(data) |
| 84 | + ) |
| 85 | + written++ |
| 86 | + } |
| 87 | + console.log(` components/*.json: ${written} files`) |
| 88 | + |
| 89 | + // 3. Full manifest (single payload with all source) |
| 90 | + const manifest: Record<string, any> = {} |
| 91 | + for (const [name, data] of components) { |
| 92 | + manifest[name] = data |
| 93 | + } |
| 94 | + writeFileSync( |
| 95 | + path.join(API_DIR, "index.json"), |
| 96 | + JSON.stringify({ |
| 97 | + generated_at: Date.now(), |
| 98 | + total: components.size, |
| 99 | + components: manifest, |
| 100 | + }) |
| 101 | + ) |
| 102 | + console.log(` index.json: full manifest`) |
| 103 | + |
| 104 | + // 4. Search index (lightweight — names + types for client-side search) |
| 105 | + const searchIndex = index.map((item) => ({ |
| 106 | + n: item.name, |
| 107 | + t: item.type, |
| 108 | + d: (item.dependencies || []).join(","), |
| 109 | + })) |
| 110 | + writeFileSync( |
| 111 | + path.join(API_DIR, "search-index.json"), |
| 112 | + JSON.stringify(searchIndex) |
| 113 | + ) |
| 114 | + console.log(` search-index.json: ${searchIndex.length} entries`) |
| 115 | + |
| 116 | + console.log("Done! Static API files written to public/api/registry/") |
| 117 | +} |
| 118 | + |
| 119 | +main() |
0 commit comments