package file→ .shtukbase/packages/keer-ui/tools/catalog.ts
tools/catalog.ts
131 lines, 7.3 KB.
- // Builds keer-ui/pages/design/keer-ui/catalog.html from keer-ui/library (names, levels, descriptions, states).
- // Usage: bun catalog.ts <keer-ui package dir>
- import { readFileSync, readdirSync, writeFileSync, mkdirSync } from 'node:fs';
- import { join } from 'node:path';
- const pkg = process.argv[2];
- const lib = join(pkg, 'library');
- const esc = (s: string) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
- type C = { file: string; name: string; level: string; doc: string; states: string[]; na: string[]; exports: string[] };
- const all: C[] = [];
- const DESC: Record<string, string> = {
- AlertDialog: 'Confirmation dialog for an irreversible action.', Alert: 'Message strip: info, warning, error.',
- Attachment: 'Attached file strip: name, size, remove.', Avatar: 'Avatar: picture or initials.',
- Badge: 'Badge: shadcn variants and soft state tones.', Breadcrumb: 'Breadcrumb trail.', Bubble: 'A speech bubble in a conversation.',
- Button: 'Button: primary graphite, outline, secondary, ghost, destructive, link.', Card: 'Card: header, content, footer.',
- Checkbox: 'Checkbox.', Collapsible: 'Collapsible block.', Dialog: 'Dialog over the page.', DropdownMenu: 'Dropdown menu.',
- Field: 'Form field: label, input, hint, error.', InputGroup: 'Input with buttons and icons inside.', Input: 'Single-line input.',
- Label: 'Field label.', Marker: 'Marker in the margin of a text.', Message: 'Conversation message: author, text, actions.',
- Progress: 'Progress bar.', RadioGroup: 'Choice of one of several.', ScrollArea: 'Area with a thin scroll bar.',
- Select: 'Select list.', Separator: 'Separator.', Sheet: 'Side sheet.', Sidebar: 'Application side panel that collapses.',
- Skeleton: 'Loading skeleton of the same shape as the data.', Sonner: 'Pop-up notifications (sonner).', Tabs: 'Tabs.',
- Textarea: 'Multi-line input.', Tooltip: 'Tooltip on hover.',
- };
- function topKeys(block: string): string[] {
- const keys: string[] = [];
- let depth = 0, expect = false, i = 0;
- while (i < block.length) {
- const rest = block.slice(i);
- const ws = rest.match(/^(\s+|\/\*[\s\S]*?\*\/|\/\/[^\n]*)/);
- if (ws) { i += ws[0].length; continue; }
- if (expect && depth === 1) {
- const m = rest.match(/^("[^"]+"|[A-Za-z][\w-]*)\s*:/);
- expect = false;
- if (m) { keys.push(m[1].replace(/"/g, '')); i += m[0].length; continue; }
- }
- const s = rest.match(/^("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`)/);
- if (s) { i += s[0].length; continue; }
- const c = block[i];
- if ('{[('.includes(c)) { depth++; if (depth === 1) expect = true; }
- else if ('}])'.includes(c)) depth--;
- else if (c === ',' && depth === 1) expect = true;
- i++;
- }
- return keys;
- }
- for (const dir of ['primitives', 'composites']) {
- for (const f of readdirSync(join(lib, dir)).filter((x) => x.endsWith('.states.tsx')).sort()) {
- const st = readFileSync(join(lib, dir, f), 'utf8');
- const src = readFileSync(join(lib, dir, f.replace('.states.tsx', '.tsx')), 'utf8');
- const name = st.match(/name:\s*"([^"]+)"/)?.[1] ?? f;
- const level = st.match(/level:\s*"([^"]+)"/)?.[1] ?? dir;
- const at = st.indexOf('defineStates');
- const open = st.indexOf('{', st.indexOf('meta,', at));
- let depth = 0, end = open;
- for (; end < st.length; end++) { if (st[end] === '{') depth++; if (st[end] === '}') { depth--; if (!depth) break; } }
- const body = st.slice(open, end + 1);
- const keys = topKeys(body);
- const naAt = body.indexOf('notApplicable');
- let na: string[] = [];
- if (naAt >= 0) {
- const o = body.indexOf('{', naAt);
- let d = 0, e = o;
- for (; e < body.length; e++) { if (body[e] === '{') d++; if (body[e] === '}') { d--; if (!d) break; } }
- na = topKeys(body.slice(o, e + 1));
- }
- const docM = src.match(new RegExp(`/\\*\\*((?:(?!\\*/)[\\s\\S])*)\\*/\\s*(?:export\\s+)?(?:function|const)\\s+${name}\\b`));
- const doc = docM ? docM[1].replace(/^\s*\*\s?/gm, '').replace(/\s+/g, ' ').trim() : (DESC[name] ?? '');
- const exports = [...src.matchAll(/^export\s+(?:function|const)\s+([A-Z]\w*)/gm)].map((m) => m[1]);
- const ex2 = [...src.matchAll(/^export\s*\{([^}]*)\}/gm)].flatMap((m) => m[1].split(',').map((s) => s.trim().split(/\s+as\s+/).pop()!.trim()).filter((s) => /^[A-Z]/.test(s)));
- all.push({ file: `${dir}/${f.replace('.states.tsx', '.tsx')}`, name, level, doc, states: keys.filter((k) => k !== 'notApplicable'), na, exports: [...new Set([...exports, ...ex2])] });
- }
- }
- const section = (lvl: string, title: string, note: string) => {
- const list = all.filter((c) => c.level === lvl);
- return `
- <section class="ks-section" id="${lvl}">
- <h2 class="ks-section-title">${title} <span class="ks-caption">${list.length}</span></h2>
- <p class="ks-muted">${note}</p>
- <div class="ks-table-wrap">
- <table class="ks-table">
- <thead><tr><th>Component</th><th>File</th><th>States</th><th>Not applicable</th></tr></thead>
- <tbody>${list.map((c) => `
- <tr>
- <td><strong>${c.name}</strong>${c.doc ? `<div class="ks-caption">${esc(c.doc)}</div>` : ''}${c.exports.length > 1 ? `<div class="ks-caption ks-mono">${esc(c.exports.join(', '))}</div>` : ''}</td>
- <td class="ks-mono">${c.file}</td>
- <td><div class="ks-row">${c.states.map((s) => `<span class="ks-badge">${esc(s)}</span>`).join('')}</div></td>
- <td><div class="ks-row">${c.na.map((s) => `<span class="ks-badge ks-badge--warning">${esc(s)}</span>`).join('')}</div></td>
- </tr>`).join('')}
- </tbody>
- </table>
- </div>
- </section>`;
- };
- const total = all.reduce((n, c) => n + c.states.length, 0);
- const html = `<!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>keer-ui — component catalog</title>
- <link rel="stylesheet" href="../keer-style/tokens.css">
- <link rel="stylesheet" href="../keer-style/elements.css">
- <style>
- .ks-table td { height: auto; padding-top: var(--ds-space-2); padding-bottom: var(--ds-space-2); vertical-align: top; }
- .ks-table td .ks-caption { max-width: 34em; }
- </style>
- </head>
- <body class="ks-page">
- <main class="ks-layout">
- <header class="ks-page-header">
- <div>
- <div class="ks-overline">keer-ui · catalog</div>
- <h1 class="ks-page-title">Components</h1>
- <p>${all.length} components and ${total} states. The code lives in the project (the folder from the target: field of the page design/keer-ui.md); the look comes from the keer-style tokens.</p>
- </div>
- </header>
- ${section('primitive', 'Primitives', 'The shadcn base on Base UI: button, input, card, table, menu, dialog. They know nothing about the product.')}
- ${section('composite', 'Composites', 'Built from primitives: page header, layout, state view, lists, badges. They know nothing about the product.')}
- <p class="ks-caption">The catalog is built from the *.states.tsx files of the library. "Not applicable" lists the states a component does not have, with the reason in its states file.</p>
- </main>
- </body>
- </html>
- `;
- mkdirSync(join(pkg, 'pages/design/keer-ui'), { recursive: true });
- writeFileSync(join(pkg, 'pages/design/keer-ui/catalog.html'), html);
- console.log(all.length, 'components', total, 'states');
- for (const c of all) if (!c.states.length || !c.doc) console.log('check:', c.name, c.states.length, c.doc ? '' : 'no doc');