|
| 1 | +import { pathExists, readJSON, writeJSON } from 'fs-extra'; |
| 2 | +import { join } from 'pathe'; |
| 3 | + |
| 4 | +import type { ComponentsJson } from '../types'; |
| 5 | + |
| 6 | +export async function readComponentsJson(cwd: string) { |
| 7 | + const file = join(cwd, 'components.json'); |
| 8 | + if (!(await pathExists(file))) return null; |
| 9 | + return (await readJSON(file)) as ComponentsJson; |
| 10 | +} |
| 11 | + |
| 12 | +export async function writeComponentsJson(cwd: string, data: ComponentsJson) { |
| 13 | + const file = join(cwd, 'components.json'); |
| 14 | + await writeJSON(file, data, { spaces: 2 }); |
| 15 | +} |
| 16 | + |
| 17 | +export function deepMerge<T extends object>(a: T, b: Partial<T>): T { |
| 18 | + const out: any = Array.isArray(a) ? [...a] : { ...a }; |
| 19 | + for (const [k, v] of Object.entries(b as any)) { |
| 20 | + if (v && typeof v === 'object' && !Array.isArray(v)) { |
| 21 | + (out as any)[k] = deepMerge((out as any)[k] ?? {}, v); |
| 22 | + } else if (v !== undefined) { |
| 23 | + (out as any)[k] = v; |
| 24 | + } |
| 25 | + } |
| 26 | + return out; |
| 27 | +} |
| 28 | + |
| 29 | +/** 生成你的默认 components.json(按你的需求) */ |
| 30 | +export function defaultComponentsJson(): ComponentsJson { |
| 31 | + return { |
| 32 | + $schema: 'https://ui.shadcn.com/schema.json', |
| 33 | + aliases: { |
| 34 | + components: '@/components', |
| 35 | + hooks: '@/hooks', |
| 36 | + lib: '@/lib', |
| 37 | + ui: '@/components/ui', |
| 38 | + utils: '@/lib/utils' |
| 39 | + }, |
| 40 | + iconLibrary: 'lucide', |
| 41 | + // 你的私有/自定义注册表 |
| 42 | + registries: { |
| 43 | + '@sr': 'http://localhost:3001/r/{name}.json' |
| 44 | + }, |
| 45 | + rsc: true, |
| 46 | + style: 'new-york', |
| 47 | + tailwind: { |
| 48 | + baseColor: 'neutral', |
| 49 | + config: '', |
| 50 | + css: 'app/globals.css', |
| 51 | + cssVariables: true, |
| 52 | + prefix: '' |
| 53 | + }, |
| 54 | + tsx: true |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * 规范化:根据 wrapper 传入的 --css-variables/--no-css-variables,落到 tailwind.cssVariables |
| 60 | + * 注意:wrapper 默认二者都 false(即不改变已有配置),只有显式传入才改。 |
| 61 | + */ |
| 62 | +export function applyCssVariablesFlag( |
| 63 | + json: ComponentsJson, |
| 64 | + flags: { cssVariables?: boolean; noCssVariables?: boolean } |
| 65 | +) { |
| 66 | + if (flags.cssVariables) { |
| 67 | + json.tailwind ??= {}; |
| 68 | + json.tailwind.cssVariables = true; |
| 69 | + } else if (flags.noCssVariables) { |
| 70 | + json.tailwind ??= {}; |
| 71 | + json.tailwind.cssVariables = false; |
| 72 | + } |
| 73 | +} |
0 commit comments