ShtukbaseStandard
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.

  1. // SBR-R03. The React library is the same set of components as the HTML library: every component file has a valid
  2. // header whose id is the file name and whose level is the folder, and, when the project has shtukbase-ui-html, every
  3. // component of it has a React file with the same level, controls, states, statuses, uses and parts (and no React
  4. // component lacks an HTML one). The header is the contract; the markup is compared by the package's own render test.
  5. import { existsSync, readFileSync } from 'node:fs';
  6. import { join } from 'node:path';
  7. import { parseHeader, type Header } from '../../design-components/lib/header.ts';
  8. import { HTML_ROOT, LEVEL_DIRS, componentFiles, config, files, readManifest } from '../actions/kitlib.ts';
  9. export const RULE = { id: 'sbr-r03', title: 'The same components as the HTML library' };
  10. const FIELDS = ['level', 'controls', 'states', 'status', 'uses', 'parts'] as const;
  11. const json = (v: unknown) => JSON.stringify(v);
  12. /** The headers of the HTML library of a project, by id: components/<level>/<id>/<id>.html. */
  13. export function htmlHeaders(root: string): { headers: Map<string, Header>; problems: string[] } {
  14. const headers = new Map<string, Header>();
  15. const problems: string[] = [];
  16. const base = join(root, HTML_ROOT, 'components');
  17. for (const rel of files(base)) {
  18. const m = /^([^/]+)\/([^/]+)\/\2\.html$/.exec(rel);
  19. if (!m) continue;
  20. const p = parseHeader(readFileSync(join(base, rel), 'utf8'), `${HTML_ROOT}/components/${rel}`);
  21. if (p.header) headers.set(p.header.id, p.header);
  22. else problems.push(...p.problems);
  23. }
  24. return { headers, problems };
  25. }
  26. export function check(root = process.cwd()): string[] {
  27. const { target } = config(root);
  28. if (!readManifest(root, target)) return []; // not placed: SBR-R01 says so
  29. const out: string[] = [];
  30. const mine = new Map<string, Header>();
  31. for (const rel of componentFiles(join(root, target))) {
  32. const where = `${target}/${rel}`;
  33. const [dir, file] = rel.split('/');
  34. const id = file.replace(/\.tsx$/, '');
  35. const p = parseHeader(readFileSync(join(root, target, rel), 'utf8'), where);
  36. if (!p.header) { out.push(...(p.problems.length ? p.problems : [`${where}: no header — the first comment must be /*--- id: …, level: … ---*/`])); continue; }
  37. if (p.header.id !== id) out.push(`${where}: the header id is "${p.header.id}", the file is "${id}"`);
  38. if (LEVEL_DIRS[p.header.level] !== dir) out.push(`${where}: level "${p.header.level}" belongs in ${LEVEL_DIRS[p.header.level]}/, not ${dir}/`);
  39. mine.set(p.header.id, p.header);
  40. }
  41. if (!existsSync(join(root, HTML_ROOT, 'components'))) return out; // no HTML library here: nothing to compare with
  42. const html = htmlHeaders(root);
  43. out.push(...html.problems);
  44. for (const [id, h] of html.headers) {
  45. const r = mine.get(id);
  46. if (!r) { out.push(`${target}/${LEVEL_DIRS[h.level]}/${id}.tsx: the HTML library has "${id}", the React library has not`); continue; }
  47. for (const f of FIELDS)
  48. 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])})`);
  49. }
  50. 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`);
  51. return out;
  52. }
  53. if (import.meta.main) {
  54. const out = check();
  55. if (out.length) console.log(out.join('\n'));
  56. process.exitCode = out.length ? 1 : 0;
  57. }