ShtukbaseStandard
package file→ .shtukbase/packages/shadcn/actions/place.ts

actions/place.ts

69 lines, 3.4 KB.

  1. // shadcn `place`: lays shadcn/ui components into the project — the code becomes the project's, as with the shadcn CLI.
  2. // Runs by itself after install and update; by hand: `shtuk run shadcn place [name ...] [--force]`.
  3. // Which components: `components:` of /design/shadcn.md, the ones placed before (.shadcn.json) and the names given;
  4. // their registry dependencies come too. With them: cn() at the `utils` alias and the theme at `tailwind.css` of
  5. // components.json (written with defaults when there is none).
  6. // A file the project has not touched since the last place is replaced by the new version; a file the project changed
  7. // (or that was there before the package) is kept and named; --force replaces it too. Nothing is ever deleted.
  8. import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
  9. import { dirname, join } from 'node:path';
  10. import {
  11. BASE_DEPS, CONFIG, MANIFEST, PAGE, REGISTRY, type Manifest, closure, components, index, page, readManifest, render,
  12. sha, targets, version,
  13. } from './shadcnlib.ts';
  14. const root = process.cwd();
  15. const force = process.argv.includes('--force');
  16. const asked = process.argv.slice(2).filter((a) => !a.startsWith('--'));
  17. let created = false;
  18. if (!existsSync(join(root, CONFIG))) {
  19. writeFileSync(join(root, CONFIG), readFileSync(join(REGISTRY, 'components.json'), 'utf8'));
  20. created = true;
  21. }
  22. const { cfg, error } = components(root);
  23. if (!cfg) {
  24. console.log(`shadcn: ${error} — fix components.json and run again`);
  25. process.exit(1);
  26. }
  27. const items = index();
  28. const old = readManifest(root);
  29. const { all, unknown } = closure([...page(root).components, ...(old?.components ?? []), ...asked], items);
  30. const t = targets(cfg, root);
  31. const plan: [string, string][] = all.map((n) => [`${t.ui}/${items[n].file}`, render(readFileSync(join(REGISTRY, 'ui', items[n].file), 'utf8'), cfg)]);
  32. plan.push([t.utils, readFileSync(join(REGISTRY, 'lib', 'utils.ts'), 'utf8')]);
  33. plan.push([t.theme, readFileSync(join(REGISTRY, 'styles', 'theme.css'), 'utf8')]);
  34. const next: Manifest = { version: version(), components: all, files: {} };
  35. let wrote = 0, same = 0;
  36. const kept: string[] = [];
  37. for (const [rel, text] of plan) {
  38. const dest = join(root, rel);
  39. const h = sha(text);
  40. if (!existsSync(dest)) {
  41. mkdirSync(dirname(dest), { recursive: true });
  42. writeFileSync(dest, text);
  43. next.files[rel] = h;
  44. wrote++;
  45. continue;
  46. }
  47. const have = sha(readFileSync(dest, 'utf8'));
  48. if (have === h) { next.files[rel] = h; same++; continue; }
  49. if (old?.files[rel] === have || force) {
  50. writeFileSync(dest, text);
  51. next.files[rel] = h;
  52. wrote++;
  53. } else {
  54. if (old?.files[rel]) next.files[rel] = old.files[rel]; // stays "changed in the project" until placed with --force
  55. kept.push(rel);
  56. }
  57. }
  58. writeFileSync(join(root, MANIFEST), `${JSON.stringify(next, null, 2)}\n`);
  59. const deps = [...new Set([...BASE_DEPS, ...all.flatMap((n) => items[n].dependencies)])].sort();
  60. console.log(`shadcn ${next.version}: ${all.length} components in ${t.ui}/ — written ${wrote}, unchanged ${same}, kept ${kept.length}`);
  61. if (created) console.log(` created ${CONFIG} (defaults: new-york, neutral, theme in ${t.theme})`);
  62. for (const k of kept) console.log(` kept, edited in the project: ${k} (replace: shtuk run shadcn place --force)`);
  63. for (const u of unknown) console.log(` not in the registry: ${u} (the list is on the page /${PAGE})`);
  64. console.log(` the project needs from npm: ${deps.join(', ')}`);