archivo del paquete→ .shtukbase/packages/shtukbase-ui-react/checks/same.ts
checks/same.ts
62 líneas, 3.4 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // SBR-R03. The React library is the same set of components as the HTML library: every component file has a valid
- // header whose id is the file name and whose level is the folder, and, when the project has shtukbase-ui-html, every
- // component of it has a React file with the same level, controls, states, statuses, uses and parts (and no React
- // component lacks an HTML one). The header is the contract; the markup is compared by the package's own render test.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { parseHeader, type Header } from '../../design-components/lib/header.ts';
- import { HTML_ROOT, LEVEL_DIRS, componentFiles, config, files, readManifest } from '../actions/kitlib.ts';
- export const RULE = { id: 'sbr-r03', title: 'The same components as the HTML library' };
- const FIELDS = ['level', 'controls', 'states', 'status', 'uses', 'parts'] as const;
- const json = (v: unknown) => JSON.stringify(v);
- /** The headers of the HTML library of a project, by id: components/<level>/<id>/<id>.html. */
- export function htmlHeaders(root: string): { headers: Map<string, Header>; problems: string[] } {
- const headers = new Map<string, Header>();
- const problems: string[] = [];
- const base = join(root, HTML_ROOT, 'components');
- for (const rel of files(base)) {
- const m = /^([^/]+)\/([^/]+)\/\2\.html$/.exec(rel);
- if (!m) continue;
- const p = parseHeader(readFileSync(join(base, rel), 'utf8'), `${HTML_ROOT}/components/${rel}`);
- if (p.header) headers.set(p.header.id, p.header);
- else problems.push(...p.problems);
- }
- return { headers, problems };
- }
- export function check(root = process.cwd()): string[] {
- const { target } = config(root);
- if (!readManifest(root, target)) return []; // not placed: SBR-R01 says so
- const out: string[] = [];
- const mine = new Map<string, Header>();
- for (const rel of componentFiles(join(root, target))) {
- const where = `${target}/${rel}`;
- const [dir, file] = rel.split('/');
- const id = file.replace(/\.tsx$/, '');
- const p = parseHeader(readFileSync(join(root, target, rel), 'utf8'), where);
- if (!p.header) { out.push(...(p.problems.length ? p.problems : [`${where}: no header — the first comment must be /*--- id: …, level: … ---*/`])); continue; }
- if (p.header.id !== id) out.push(`${where}: the header id is "${p.header.id}", the file is "${id}"`);
- if (LEVEL_DIRS[p.header.level] !== dir) out.push(`${where}: level "${p.header.level}" belongs in ${LEVEL_DIRS[p.header.level]}/, not ${dir}/`);
- mine.set(p.header.id, p.header);
- }
- if (!existsSync(join(root, HTML_ROOT, 'components'))) return out; // no HTML library here: nothing to compare with
- const html = htmlHeaders(root);
- out.push(...html.problems);
- for (const [id, h] of html.headers) {
- const r = mine.get(id);
- if (!r) { out.push(`${target}/${LEVEL_DIRS[h.level]}/${id}.tsx: the HTML library has "${id}", the React library has not`); continue; }
- for (const f of FIELDS)
- if (json(r[f]) !== json(h[f])) out.push(`${target}/${LEVEL_DIRS[r.level]}/${id}.tsx: header ${f} differs from the HTML library (${json(r[f])} against ${json(h[f])})`);
- }
- for (const id of mine.keys()) if (!html.headers.has(id)) out.push(`${target}: "${id}" is not in the HTML library — the React library has the same ids, no more`);
- return out;
- }
- if (import.meta.main) {
- const out = check();
- if (out.length) console.log(out.join('\n'));
- process.exitCode = out.length ? 1 : 0;
- }