ShtukbaseStandard
包文件→ .shtukbase/packages/shtukbase-ui-react/actions/place.ts

actions/place.ts

48 行, 2.5 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // shtukbase-ui-react `place`: copies the component library into the project (like shadcn and keer-ui: the code becomes
  2. // the project's). Runs by itself after install and update; by hand: `shtuk run shtukbase-ui-react 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 { CONFIG, LIBRARY, MANIFEST, type Manifest, config, files, readManifest, registerSource, render, sha, version } from './kitlib.ts';
  8. const root = process.cwd();
  9. const force = process.argv.includes('--force');
  10. const { target, css } = config(root);
  11. const old = readManifest(root, target);
  12. const next: Manifest = { version: version(), css, 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, css);
  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(`shtukbase-ui-react ${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 shtukbase-ui-react place --force)`);
  42. if (!existsSync(join(root, css))) console.log(` note: the CSS file ${css} is missing — install shtukbase-ui-html or set css: in /${CONFIG}`);
  43. const source = registerSource(root, target);
  44. if (source === 'added') console.log(` added ${target} to sources: of /design/components.md, so that design-components sync lists these components`);
  45. if (source === 'by-hand') console.log(` note: add ${target} to sources: of /design/components.md by hand (design-components sync reads the components from there)`);