ShtukbaseStandard
包文件→ .shtukbase/packages/shtukbase-ui-react/tools/sync-headers.ts

tools/sync-headers.ts

33 行, 1.9 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // Copies the headers of the HTML library into the React components: the header is the contract and the same text in both
  2. // libraries (rule SBR-R03). A maintainer's tool: run it when the HTML library changes a header, then adjust the props.
  3. // Usage, from the package folder: bun tools/sync-headers.ts [path to the HTML library's components folder]
  4. import { existsSync, readFileSync, writeFileSync } from 'node:fs';
  5. import { join } from 'node:path';
  6. import { LEVEL_DIRS, files } from '../actions/kitlib.ts';
  7. import { findHeader, parseHeader } from '../../design-components/lib/header.ts';
  8. const pkg = join(import.meta.dir, '..');
  9. const from = process.argv[2] ?? join(pkg, '..', 'shtukbase-ui-html', 'pages', 'design', 'shtukbase-ui-html', 'components');
  10. if (!existsSync(from)) {
  11. console.error(`no HTML components folder at ${from}`);
  12. process.exit(1);
  13. }
  14. let changed = 0;
  15. for (const rel of files(from)) {
  16. const m = /^([^/]+)\/([^/]+)\/\2\.html$/.exec(rel);
  17. if (!m) continue;
  18. const text = readFileSync(join(from, rel), 'utf8');
  19. const parsed = parseHeader(text, rel);
  20. const found = findHeader(text);
  21. if (!parsed.header || !found) { console.error(`${rel}: ${parsed.problems.join('; ') || 'no header'}`); process.exitCode = 1; continue; }
  22. const target = join(pkg, 'library', LEVEL_DIRS[parsed.header.level], `${parsed.header.id}.tsx`);
  23. if (!existsSync(target)) { console.error(`${rel}: no React component ${target}`); process.exitCode = 1; continue; }
  24. const code = readFileSync(target, 'utf8');
  25. const mine = findHeader(code);
  26. if (!mine) { console.error(`${target}: no header comment`); process.exitCode = 1; continue; }
  27. const next = `/*---\n${found.raw.replace(/^\s*\n/, '').replace(/\s+$/, '')}\n---*/`;
  28. const out = code.slice(0, mine.start) + next + code.slice(mine.end);
  29. if (out !== code) { writeFileSync(target, out); changed++; console.log(`updated ${parsed.header.id}`); }
  30. }
  31. console.log(`${changed} header(s) updated`);