archivo del paquete→ .shtukbase/packages/shadcn/actions/shadcnlib.ts
actions/shadcnlib.ts
128 líneas, 5.9 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // Shared parts of the shadcn action and checks. The working directory is the project root.
- // The registry (vendored shadcn/ui code) lives in .shtukbase/packages/shadcn/registry. `place` copies the chosen
- // components into the folder of the `ui` alias of components.json, cn() into the `utils` alias, the theme into
- // `tailwind.css`, and writes .shadcn.json: the components placed and the hash of every file it laid.
- import { createHash } from 'node:crypto';
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { readPage } from '../../collections/checks/lib/pages.ts';
- export const PAGE = 'design/shadcn.md';
- export const CONFIG = 'components.json';
- export const MANIFEST = '.shadcn.json';
- export const REGISTRY = join(import.meta.dir, '..', 'registry');
- export const SCHEMA_URL = 'https://ui.shadcn.com/schema.json';
- /** What every component needs from npm (beyond each item's own dependencies). */
- export const BASE_DEPS = ['react', 'cn', 'class-variance-authority', 'lucide-react', 'tailwindcss@4', 'tw-animate-css'];
- export type Item = { file: string; dependencies: string[]; registryDependencies: string[] };
- export type Manifest = { version: string; components: string[]; files: Record<string, string> };
- export type Components = {
- $schema?: string; style?: string; rsc?: boolean; tsx?: boolean;
- tailwind?: { config?: string; css?: string; baseColor?: string; cssVariables?: boolean; prefix?: string };
- aliases?: Record<string, string>; iconLibrary?: string; [k: string]: unknown;
- };
- export function sha(text: string): string {
- return createHash('sha256').update(text).digest('hex');
- }
- /** JSON with // and /* comments and trailing commas (tsconfig.json). */
- export function readJsonc(path: string): any {
- const text = readFileSync(path, 'utf8')
- .replace(/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (m, s) => s ?? '')
- .replace(/,(\s*[}\]])/g, '$1');
- return JSON.parse(text);
- }
- export function index(): Record<string, Item> {
- return JSON.parse(readFileSync(join(REGISTRY, 'index.json'), 'utf8')).items;
- }
- export function page(root = process.cwd()): { components: string[]; aliasRoot: string | null } {
- const p = join(root, PAGE);
- const fm = existsSync(p) ? readPage(p).fm : {};
- const list = Array.isArray(fm.components) ? fm.components.map(String) : [];
- return { components: list, aliasRoot: typeof fm.aliasRoot === 'string' && fm.aliasRoot.trim() ? fm.aliasRoot.trim().replace(/^\.?\/+|\/+$/g, '') : null };
- }
- /** components.json, parsed; `error` when it is missing or not JSON. */
- export function components(root = process.cwd()): { cfg: Components | null; error: string | null } {
- const p = join(root, CONFIG);
- if (!existsSync(p)) return { cfg: null, error: 'no components.json' };
- try {
- const cfg = JSON.parse(readFileSync(p, 'utf8'));
- return cfg && typeof cfg === 'object' && !Array.isArray(cfg) ? { cfg, error: null } : { cfg: null, error: 'components.json is not a JSON object' };
- } catch (e) {
- return { cfg: null, error: `components.json cannot be read as JSON: ${(e as Error).message}` };
- }
- }
- /** Where the `@/` of aliases points: aliasRoot of the page, else tsconfig paths "@/*", else src. */
- export function aliasRoot(root = process.cwd()): string {
- const own = page(root).aliasRoot;
- if (own) return own;
- for (const name of ['tsconfig.json', 'jsconfig.json']) {
- const p = join(root, name);
- if (!existsSync(p)) continue;
- try {
- const target = readJsonc(p)?.compilerOptions?.paths?.['@/*']?.[0];
- if (typeof target === 'string') return target.replace(/^\.\/?/, '').replace(/\/?\*$/, '');
- } catch { /* unreadable tsconfig: fall through */ }
- }
- return 'src';
- }
- /** An alias (@/components/ui) → a project folder (src/components/ui). A path without @/ is taken as it is. */
- export function aliasPath(alias: string, root = process.cwd()): string {
- const r = aliasRoot(root);
- const rest = alias.replace(/^[@~]\//, '');
- return /^[@~]\//.test(alias) ? (r ? `${r}/${rest}` : rest) : alias.replace(/^\.?\/+/, '');
- }
- export function aliases(cfg: Components): { ui: string; utils: string } {
- const a = cfg.aliases ?? {};
- return { ui: a.ui ?? `${a.components ?? '@/components'}/ui`, utils: a.utils ?? '@/lib/utils' };
- }
- /** The names and every registry dependency of them, in a stable order. Unknown names are returned apart. */
- export function closure(names: string[], items: Record<string, Item>): { all: string[]; unknown: string[] } {
- const seen = new Set<string>();
- const unknown: string[] = [];
- const visit = (n: string) => {
- if (seen.has(n)) return;
- if (!items[n]) { if (!unknown.includes(n)) unknown.push(n); return; }
- seen.add(n);
- for (const d of items[n].registryDependencies) visit(d);
- };
- for (const n of names) visit(n);
- return { all: [...seen].sort(), unknown };
- }
- /** A registry file as it lies in the project: imports point at the project's aliases. */
- export function render(text: string, cfg: Components): string {
- const a = aliases(cfg);
- return text.split('@/components/ui/').join(`${a.ui}/`).split('"@/lib/utils"').join(`"${a.utils}"`);
- }
- export function readManifest(root = process.cwd()): Manifest | null {
- const p = join(root, MANIFEST);
- if (!existsSync(p)) return null;
- try {
- return JSON.parse(readFileSync(p, 'utf8')) as Manifest;
- } catch {
- return null;
- }
- }
- export function version(): string {
- const p = join(import.meta.dir, '..', 'package.md');
- return existsSync(p) ? String(readPage(p).fm.version ?? '') : '';
- }
- /** Where the package's files go in this project: ui folder, utils file, theme file. */
- export function targets(cfg: Components, root = process.cwd()): { ui: string; utils: string; theme: string } {
- const a = aliases(cfg);
- const utils = aliasPath(a.utils, root);
- return { ui: aliasPath(a.ui, root), utils: /\.[jt]sx?$/.test(utils) ? utils : `${utils}.ts`, theme: (cfg.tailwind?.css ?? 'src/styles/globals.css').replace(/^\.?\/+/, '') };
- }