ShtukbaseStandard
файл пакета→ .shtukbase/packages/keer-ui/actions/kitlib.ts

actions/kitlib.ts

61 строка, 2.7 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // Shared parts of the keer-ui action and checks. The working directory is the project root.
  2. // The library lives in .shtukbase/packages/keer-ui/library; `place` copies it into the project folder named by
  3. // `target:` of /design/keer-ui.md (default src/ui) and writes <target>/.keer-ui.json: the hash of every file it laid.
  4. import { createHash } from 'node:crypto';
  5. import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
  6. import { dirname, join, relative } from 'node:path';
  7. import { readPage } from '../../collections/checks/lib/pages.ts';
  8. export const CONFIG = 'design/keer-ui.md';
  9. export const MANIFEST = '.keer-ui.json';
  10. export const LIBRARY = join(import.meta.dir, '..', 'library');
  11. export type Manifest = { version: string; tokens: string; files: Record<string, string> };
  12. export function sha(text: string): string {
  13. return createHash('sha256').update(text).digest('hex');
  14. }
  15. /** target and tokens paths (relative to the project root) from the config page, with defaults. */
  16. export function config(root = process.cwd()): { target: string; tokens: string } {
  17. const page = join(root, CONFIG);
  18. const fm = existsSync(page) ? readPage(page).fm : {};
  19. const clean = (v: unknown, d: string) => (typeof v === 'string' && v.trim() ? v.trim().replace(/^\/+|\/+$/g, '') : d);
  20. return { target: clean(fm.target, 'src/ui'), tokens: clean(fm.tokens, 'design/keer-style/tokens.css') };
  21. }
  22. /** Every file under a folder, relative paths with forward slashes. */
  23. export function files(dir: string, rel = ''): string[] {
  24. if (!existsSync(join(dir, rel))) return [];
  25. const out: string[] = [];
  26. for (const name of readdirSync(join(dir, rel)).sort()) {
  27. const r = rel ? `${rel}/${name}` : name;
  28. if (statSync(join(dir, r)).isDirectory()) out.push(...files(dir, r));
  29. else out.push(r);
  30. }
  31. return out;
  32. }
  33. /** The text of a library file as it lies in the project: the tokens import points at the project's tokens file. */
  34. export function render(rel: string, text: string, target: string, tokens: string): string {
  35. if (!text.includes('{{tokens}}')) return text;
  36. let path = relative(dirname(join(target, rel)), tokens).split('\\').join('/');
  37. if (!path.startsWith('.')) path = `./${path}`;
  38. return text.split('{{tokens}}').join(path);
  39. }
  40. export function readManifest(root: string, target: string): Manifest | null {
  41. const p = join(root, target, MANIFEST);
  42. if (!existsSync(p)) return null;
  43. try {
  44. return JSON.parse(readFileSync(p, 'utf8')) as Manifest;
  45. } catch {
  46. return null;
  47. }
  48. }
  49. /** The package version from its package.md (for the manifest). */
  50. export function version(): string {
  51. const p = join(import.meta.dir, '..', 'package.md');
  52. return existsSync(p) ? String(readPage(p).fm.version ?? '') : '';
  53. }