ShtukbaseStandard
package file→ .shtukbase/packages/shadcn/tools/vendor.ts

tools/vendor.ts

38 lines, 2.3 KB.

  1. // Refreshes the vendored shadcn/ui components from the public registry (a maintainer's tool, never run by install).
  2. // Usage, from the package folder: bun tools/vendor.ts [name ...] (no names: the names already in registry/index.json)
  3. // Reads https://ui.shadcn.com/r/styles/new-york-v4/<name>.json and writes registry/ui/<file> and registry/index.json.
  4. // Imports are written with the standard aliases (@/components/ui/x, @/lib/utils); `place` maps them to the project's.
  5. import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
  6. import { join } from 'node:path';
  7. const STYLE = 'new-york-v4';
  8. const BASE = `https://ui.shadcn.com/r/styles/${STYLE}`;
  9. const DIR = join(import.meta.dir, '..', 'registry');
  10. type Item = { file: string; dependencies: string[]; registryDependencies: string[] };
  11. const indexPath = join(DIR, 'index.json');
  12. const old = existsSync(indexPath) ? JSON.parse(readFileSync(indexPath, 'utf8')) : { items: {} };
  13. const names = process.argv.slice(2).length ? process.argv.slice(2) : Object.keys(old.items);
  14. const items: Record<string, Item> = { ...old.items };
  15. for (const name of names) {
  16. const res = await fetch(`${BASE}/${name}.json`);
  17. if (!res.ok) { console.error(`${name}: HTTP ${res.status}`); process.exitCode = 1; continue; }
  18. const j = (await res.json()) as { files: { path: string; content: string }[]; dependencies?: string[]; registryDependencies?: string[] };
  19. const f = j.files.find((x) => x.path.includes('/ui/'));
  20. if (!f) { console.error(`${name}: no ui file`); continue; }
  21. const file = f.path.split('/').pop() as string;
  22. const text = f.content
  23. .replace(new RegExp(`@/registry/${STYLE}/ui/`, 'g'), '@/components/ui/')
  24. .replace(/from "cn"/g, 'from "@/lib/utils"');
  25. mkdirSync(join(DIR, 'ui'), { recursive: true });
  26. writeFileSync(join(DIR, 'ui', file), text);
  27. items[name] = {
  28. file,
  29. dependencies: (j.dependencies ?? []).filter((d) => d !== 'cn'),
  30. registryDependencies: (j.registryDependencies ?? []).map((d) => d.split('/').pop()?.replace(/\.json$/, '') ?? d),
  31. };
  32. console.log(`${name}: ${file}`);
  33. }
  34. const sorted = Object.fromEntries(Object.entries(items).sort(([a], [b]) => a.localeCompare(b)));
  35. 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`);