ShtukbaseStandard
package file→ .shtukbase/packages/shtukbase-ui-react/actions/kitlib.ts

actions/kitlib.ts

90 lines, 4.4 KB.

  1. // Shared parts of the shtukbase-ui-react action and checks. The working directory is the project root.
  2. // The library lives in .shtukbase/packages/shtukbase-ui-react/library; `place` copies it into the project folder named by
  3. // `target:` of /design/shtukbase-ui-react.md (default src/sb-ui) and writes <target>/.shtukbase-ui-react.json: the hash of
  4. // every file it laid.
  5. import { createHash } from 'node:crypto';
  6. import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
  7. import { dirname, join, relative } from 'node:path';
  8. import { readPage } from '../../collections/checks/lib/pages.ts';
  9. export const CONFIG = 'design/shtukbase-ui-react.md';
  10. export const MANIFEST = '.shtukbase-ui-react.json';
  11. export const LIBRARY = join(import.meta.dir, '..', 'library');
  12. /** Where the HTML library lays its components and its joined CSS (the design: one look, written once, in its CSS). */
  13. export const HTML_ROOT = 'design/shtukbase-ui-html';
  14. /** Level of a component -> the folder of the library it lives in. */
  15. export const LEVEL_DIRS: Record<string, string> = { primitive: 'primitives', composite: 'composites', block: 'blocks', shell: 'shell', page: 'pages' };
  16. export type Manifest = { version: string; css: string; files: Record<string, string> };
  17. export function sha(text: string): string {
  18. return createHash('sha256').update(text).digest('hex');
  19. }
  20. /** target and css paths (relative to the project root) from the config page, with defaults. */
  21. export function config(root = process.cwd()): { target: string; css: string } {
  22. const page = join(root, CONFIG);
  23. const fm = existsSync(page) ? readPage(page).fm : {};
  24. const clean = (v: unknown, d: string) => (typeof v === 'string' && v.trim() ? v.trim().replace(/^\/+|\/+$/g, '') : d);
  25. return { target: clean(fm.target, 'src/sb-ui'), css: clean(fm.css, `${HTML_ROOT}/ui.css`) };
  26. }
  27. /** Every file under a folder, relative paths with forward slashes. */
  28. export function files(dir: string, rel = ''): string[] {
  29. if (!existsSync(join(dir, rel))) return [];
  30. const out: string[] = [];
  31. for (const name of readdirSync(join(dir, rel)).sort()) {
  32. const r = rel ? `${rel}/${name}` : name;
  33. if (statSync(join(dir, r)).isDirectory()) out.push(...files(dir, r));
  34. else out.push(r);
  35. }
  36. return out;
  37. }
  38. /** The text of a library file as it lies in the project: the CSS import points at the project's `ui.css`. */
  39. export function render(rel: string, text: string, target: string, css: string): string {
  40. if (!text.includes('{{css}}')) return text;
  41. let path = relative(dirname(join(target, rel)), css).split('\\').join('/');
  42. if (!path.startsWith('.')) path = `./${path}`;
  43. return text.split('{{css}}').join(path);
  44. }
  45. export function readManifest(root: string, target: string): Manifest | null {
  46. const p = join(root, target, MANIFEST);
  47. if (!existsSync(p)) return null;
  48. try {
  49. return JSON.parse(readFileSync(p, 'utf8')) as Manifest;
  50. } catch {
  51. return null;
  52. }
  53. }
  54. /** The package version from its package.md (for the manifest). */
  55. export function version(): string {
  56. const p = join(import.meta.dir, '..', 'package.md');
  57. return existsSync(p) ? String(readPage(p).fm.version ?? '') : '';
  58. }
  59. /** The component files of a folder: `<level dir>/<id>.tsx`, not states, tests, stories; lib/, hooks/, styles/ are not components. */
  60. export function componentFiles(dir: string): string[] {
  61. return files(dir).filter((rel) => /^(primitives|composites|blocks|shell|pages)\/[^/]+\.tsx$/.test(rel) && !/\.(states|test|spec|stories)\.tsx$/.test(rel));
  62. }
  63. /**
  64. * Lets `design-components sync` find the placed components: adds the target folder to `sources:` in the header of
  65. * /design/components.md (a one-line list there: `sources: []`, `sources: [a, b]`). Returns what happened, for the log.
  66. */
  67. export function registerSource(root: string, target: string): 'added' | 'present' | 'no-page' | 'by-hand' {
  68. const path = join(root, 'design/components.md');
  69. if (!existsSync(path)) return 'no-page';
  70. const text = readFileSync(path, 'utf8');
  71. const head = /^---\n[\s\S]*?\n---/.exec(text);
  72. if (!head) return 'by-hand';
  73. const m = /^sources:[ \t]*\[([^\]\n]*)\][ \t]*$/m.exec(head[0]);
  74. if (!m) return 'by-hand';
  75. const items = m[1].split(',').map((x) => x.trim().replace(/^["']|["']$/g, '').replace(/^\/+|\/+$/g, '')).filter(Boolean);
  76. if (items.includes(target)) return 'present';
  77. const line = `sources: [${[...items, target].join(', ')}]`;
  78. writeFileSync(path, text.replace(head[0], head[0].replace(m[0], line)));
  79. return 'added';
  80. }