ShtukbaseStandard
package file→ .shtukbase/packages/design-components/checks/fresh.ts

checks/fresh.ts

35 lines, 2.1 KB.

  1. // DSC-R01. The pages, the data file, the manifest and the stories are what `sync` writes from the component headers now.
  2. // The text under a page's header is the author's and is not compared; the generated keys of the header are.
  3. import { existsSync, readFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { canonicalHead, plan, splitPage } from '../lib/outputs.ts';
  6. import { collect } from '../lib/project.ts';
  7. import { report } from '../lib/report.ts';
  8. export const RULE = { id: 'dsc-r01', title: 'Component pages and generated files are fresh' };
  9. const FIX = 'run: shtuk run design-components sync';
  10. export function check(root = process.cwd()): string[] {
  11. const col = collect(root);
  12. if (col.problems.length) return col.problems.map((p) => `${p} (sync cannot run until the header is fixed)`);
  13. const p = plan(root, col);
  14. const out: string[] = [];
  15. for (const page of p.pages) {
  16. const abs = join(root, page.rel);
  17. if (!existsSync(abs)) { out.push(`${page.rel}: the page is missing — ${FIX}`); continue; }
  18. const parts = splitPage(readFileSync(abs, 'utf8'));
  19. let have: Record<string, unknown> = {};
  20. try { have = ((parts ? Bun.YAML.parse(parts.head) : {}) ?? {}) as Record<string, unknown>; } catch { /* compared as empty */ }
  21. if (canonicalHead(have) !== canonicalHead(page.head)) out.push(`${page.rel}: the header does not match the component header in its source — ${FIX} (the text under the header stays)`);
  22. }
  23. for (const [rel, text] of p.files) {
  24. const abs = join(root, rel);
  25. if (!existsSync(abs)) out.push(`${rel}: the file is missing — ${FIX}`);
  26. else if (readFileSync(abs, 'utf8') !== text) out.push(`${rel}: does not match the component headers — ${FIX} (this file is not edited by hand)`);
  27. }
  28. for (const rel of p.orphanPages) out.push(`${rel}: its component has no header any more — restore the header, or delete the page (shtuk run design-components sync --prune)`);
  29. for (const rel of p.orphanStories) out.push(`${rel}: its component has no header or templates any more — ${FIX}`);
  30. return out;
  31. }
  32. if (import.meta.main) report(check());