файл пакета→ .shtukbase/packages/design-components/checks/fresh.ts
checks/fresh.ts
35 строк, 2.1 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // DSC-R01. The pages, the data file, the manifest and the stories are what `sync` writes from the component headers now.
- // The text under a page's header is the author's and is not compared; the generated keys of the header are.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { canonicalHead, plan, splitPage } from '../lib/outputs.ts';
- import { collect } from '../lib/project.ts';
- import { report } from '../lib/report.ts';
- export const RULE = { id: 'dsc-r01', title: 'Component pages and generated files are fresh' };
- const FIX = 'run: shtuk run design-components sync';
- export function check(root = process.cwd()): string[] {
- const col = collect(root);
- if (col.problems.length) return col.problems.map((p) => `${p} (sync cannot run until the header is fixed)`);
- const p = plan(root, col);
- const out: string[] = [];
- for (const page of p.pages) {
- const abs = join(root, page.rel);
- if (!existsSync(abs)) { out.push(`${page.rel}: the page is missing — ${FIX}`); continue; }
- const parts = splitPage(readFileSync(abs, 'utf8'));
- let have: Record<string, unknown> = {};
- try { have = ((parts ? Bun.YAML.parse(parts.head) : {}) ?? {}) as Record<string, unknown>; } catch { /* compared as empty */ }
- 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)`);
- }
- for (const [rel, text] of p.files) {
- const abs = join(root, rel);
- if (!existsSync(abs)) out.push(`${rel}: the file is missing — ${FIX}`);
- else if (readFileSync(abs, 'utf8') !== text) out.push(`${rel}: does not match the component headers — ${FIX} (this file is not edited by hand)`);
- }
- 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)`);
- for (const rel of p.orphanStories) out.push(`${rel}: its component has no header or templates any more — ${FIX}`);
- return out;
- }
- if (import.meta.main) report(check());