|
| 1 | +import path from 'node:path'; |
| 2 | + |
| 3 | +import fg from 'fast-glob'; |
| 4 | +import { type Registry } from 'shadcn/registry'; |
| 5 | + |
| 6 | +import { registryComponentsDependencies } from './constants'; |
| 7 | + |
| 8 | +const COMPONENTS_DIR = 'src/components'; |
| 9 | + |
| 10 | +function formatComponentName(str: string): string { |
| 11 | + return str |
| 12 | + .split('-') |
| 13 | + .map(part => part.charAt(0).toUpperCase() + part.slice(1)) |
| 14 | + .join(' '); |
| 15 | +} |
| 16 | + |
| 17 | +function getGroupedComponents(): Record<string, string[]> { |
| 18 | + const components = fg.sync(`${COMPONENTS_DIR}/**/*`); |
| 19 | + const grouped: Record<string, string[]> = {}; |
| 20 | + |
| 21 | + for (const fullPath of components) { |
| 22 | + const segments = fullPath.split(path.sep); |
| 23 | + const group = segments[2]; // 如 'accordion' |
| 24 | + const name = segments[3]; // 文件名 |
| 25 | + |
| 26 | + // eslint-disable-next-line no-continue |
| 27 | + if (!group || !name) continue; |
| 28 | + |
| 29 | + if (!grouped[group]) grouped[group] = []; |
| 30 | + grouped[group].push(name); |
| 31 | + } |
| 32 | + |
| 33 | + return grouped; |
| 34 | +} |
| 35 | + |
| 36 | +function generateRegistryItems(grouped: Record<string, string[]>): Registry['items'] { |
| 37 | + const ui = Object.entries(grouped).map(([group, files]) => { |
| 38 | + const item = { |
| 39 | + files: files.map(file => ({ |
| 40 | + path: `${COMPONENTS_DIR}/${group}/${file}`, |
| 41 | + target: `components/${group}/${file}`, |
| 42 | + type: 'registry:ui' |
| 43 | + })), |
| 44 | + name: group, |
| 45 | + title: formatComponentName(group), |
| 46 | + type: 'registry:block' |
| 47 | + }; |
| 48 | + |
| 49 | + if (registryComponentsDependencies[group]) { |
| 50 | + Object.assign(item, registryComponentsDependencies[group]); |
| 51 | + } |
| 52 | + |
| 53 | + return item; |
| 54 | + }) as Registry['items']; |
| 55 | + |
| 56 | + console.log('🎨 UI component conversion completed.'); |
| 57 | + |
| 58 | + return ui; |
| 59 | +} |
| 60 | + |
| 61 | +export function getRegistryUi(): Registry['items'] { |
| 62 | + console.log('📦 Starting to collect components for registry...'); |
| 63 | + const grouped = getGroupedComponents(); |
| 64 | + |
| 65 | + console.log('✅ Component collection completed.'); |
| 66 | + |
| 67 | + return generateRegistryItems(grouped); |
| 68 | +} |
0 commit comments