package file→ .shtukbase/packages/shtukbase-ui-react/tools/sync-headers.ts
tools/sync-headers.ts
33 lines, 1.9 KB.
- // Copies the headers of the HTML library into the React components: the header is the contract and the same text in both
- // libraries (rule SBR-R03). A maintainer's tool: run it when the HTML library changes a header, then adjust the props.
- // Usage, from the package folder: bun tools/sync-headers.ts [path to the HTML library's components folder]
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { LEVEL_DIRS, files } from '../actions/kitlib.ts';
- import { findHeader, parseHeader } from '../../design-components/lib/header.ts';
- const pkg = join(import.meta.dir, '..');
- const from = process.argv[2] ?? join(pkg, '..', 'shtukbase-ui-html', 'pages', 'design', 'shtukbase-ui-html', 'components');
- if (!existsSync(from)) {
- console.error(`no HTML components folder at ${from}`);
- process.exit(1);
- }
- let changed = 0;
- for (const rel of files(from)) {
- const m = /^([^/]+)\/([^/]+)\/\2\.html$/.exec(rel);
- if (!m) continue;
- const text = readFileSync(join(from, rel), 'utf8');
- const parsed = parseHeader(text, rel);
- const found = findHeader(text);
- if (!parsed.header || !found) { console.error(`${rel}: ${parsed.problems.join('; ') || 'no header'}`); process.exitCode = 1; continue; }
- const target = join(pkg, 'library', LEVEL_DIRS[parsed.header.level], `${parsed.header.id}.tsx`);
- if (!existsSync(target)) { console.error(`${rel}: no React component ${target}`); process.exitCode = 1; continue; }
- const code = readFileSync(target, 'utf8');
- const mine = findHeader(code);
- if (!mine) { console.error(`${target}: no header comment`); process.exitCode = 1; continue; }
- const next = `/*---\n${found.raw.replace(/^\s*\n/, '').replace(/\s+$/, '')}\n---*/`;
- const out = code.slice(0, mine.start) + next + code.slice(mine.end);
- if (out !== code) { writeFileSync(target, out); changed++; console.log(`updated ${parsed.header.id}`); }
- }
- console.log(`${changed} header(s) updated`);