ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/keer-ui/actions/place.ts

actions/place.ts

45 líneas, 2.1 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // keer-ui `place`: copies the component library into the project (like shadcn: the code becomes the project's).
  2. // Runs by itself after install and update; by hand: `shtuk run keer-ui place [--force]`.
  3. // A file the project has not touched since the last place is replaced by the new version; a file the project
  4. // changed is kept and named (with --force it is replaced too). Files are never deleted.
  5. import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
  6. import { dirname, join } from 'node:path';
  7. import { LIBRARY, MANIFEST, type Manifest, config, files, readManifest, render, sha, version } from './kitlib.ts';
  8. const root = process.cwd();
  9. const force = process.argv.includes('--force');
  10. const { target, tokens } = config(root);
  11. const old = readManifest(root, target);
  12. const next: Manifest = { version: version(), tokens, files: {} };
  13. let wrote = 0, same = 0;
  14. const kept: string[] = [];
  15. for (const rel of files(LIBRARY)) {
  16. const text = render(rel, readFileSync(join(LIBRARY, rel), 'utf8'), target, tokens);
  17. const dest = join(root, target, rel);
  18. const h = sha(text);
  19. if (!existsSync(dest)) {
  20. mkdirSync(dirname(dest), { recursive: true });
  21. writeFileSync(dest, text);
  22. next.files[rel] = h;
  23. wrote++;
  24. continue;
  25. }
  26. const have = sha(readFileSync(dest, 'utf8'));
  27. if (have === h) { next.files[rel] = h; same++; continue; }
  28. const clean = old?.files[rel] === have;
  29. if (clean || force) {
  30. writeFileSync(dest, text);
  31. next.files[rel] = h;
  32. wrote++;
  33. } else {
  34. next.files[rel] = old?.files[rel] ?? h; // stays "changed in the project" until placed with --force
  35. kept.push(rel);
  36. }
  37. }
  38. mkdirSync(join(root, target), { recursive: true });
  39. writeFileSync(join(root, target, MANIFEST), `${JSON.stringify(next, null, 2)}\n`);
  40. console.log(`keer-ui ${next.version}: ${target}/ — written ${wrote}, unchanged ${same}, kept ${kept.length}`);
  41. for (const k of kept) console.log(` kept, changed in the project: ${target}/${k} (replace: shtuk run keer-ui place --force)`);
  42. if (!existsSync(join(root, tokens))) console.log(` note: tokens file ${tokens} is missing — install keer-style or set tokens: in ${'design/keer-ui.md'}`);