файл пакета→ .shtukbase/packages/shadcn/tools/vendor.ts
tools/vendor.ts
38 строк, 2.3 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // Refreshes the vendored shadcn/ui components from the public registry (a maintainer's tool, never run by install).
- // Usage, from the package folder: bun tools/vendor.ts [name ...] (no names: the names already in registry/index.json)
- // Reads https://ui.shadcn.com/r/styles/new-york-v4/<name>.json and writes registry/ui/<file> and registry/index.json.
- // Imports are written with the standard aliases (@/components/ui/x, @/lib/utils); `place` maps them to the project's.
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- const STYLE = 'new-york-v4';
- const BASE = `https://ui.shadcn.com/r/styles/${STYLE}`;
- const DIR = join(import.meta.dir, '..', 'registry');
- type Item = { file: string; dependencies: string[]; registryDependencies: string[] };
- const indexPath = join(DIR, 'index.json');
- const old = existsSync(indexPath) ? JSON.parse(readFileSync(indexPath, 'utf8')) : { items: {} };
- const names = process.argv.slice(2).length ? process.argv.slice(2) : Object.keys(old.items);
- const items: Record<string, Item> = { ...old.items };
- for (const name of names) {
- const res = await fetch(`${BASE}/${name}.json`);
- if (!res.ok) { console.error(`${name}: HTTP ${res.status}`); process.exitCode = 1; continue; }
- const j = (await res.json()) as { files: { path: string; content: string }[]; dependencies?: string[]; registryDependencies?: string[] };
- const f = j.files.find((x) => x.path.includes('/ui/'));
- if (!f) { console.error(`${name}: no ui file`); continue; }
- const file = f.path.split('/').pop() as string;
- const text = f.content
- .replace(new RegExp(`@/registry/${STYLE}/ui/`, 'g'), '@/components/ui/')
- .replace(/from "cn"/g, 'from "@/lib/utils"');
- mkdirSync(join(DIR, 'ui'), { recursive: true });
- writeFileSync(join(DIR, 'ui', file), text);
- items[name] = {
- file,
- dependencies: (j.dependencies ?? []).filter((d) => d !== 'cn'),
- registryDependencies: (j.registryDependencies ?? []).map((d) => d.split('/').pop()?.replace(/\.json$/, '') ?? d),
- };
- console.log(`${name}: ${file}`);
- }
- const sorted = Object.fromEntries(Object.entries(items).sort(([a], [b]) => a.localeCompare(b)));
- writeFileSync(indexPath, `${JSON.stringify({ source: BASE, license: 'MIT, (c) 2023 shadcn — see LICENSE.md', fetched: new Date().toISOString().slice(0, 10), items: sorted }, null, 2)}\n`);