package file→ .shtukbase/packages/tui/checks/doctor.ts
checks/doctor.ts
75 lines, 3.5 KB.
- // Sensor (only watches; the check never fails): is the terminal front here, which version, does it read this project
- // (the sidebar is not empty), and what of the project it does not show yet: collections and saved boards
- // (`views` with `layout: board`) missing from the sidebar. Run: `shtuk run tui doctor [--own-node]`.
- import { join } from 'node:path';
- import { readPage, walk } from '../../collections/checks/lib/pages.ts';
- import { INSTALL_HINT, findBin, front, reachNode, render } from '../lib/tui.ts';
- const root = process.cwd();
- /** Collection pages of the project (front matter `type: Collection`), outside dot folders: title and board views. */
- function collections(): { addr: string; title: string; boards: string[] }[] {
- const out: { addr: string; title: string; boards: string[] }[] = [];
- for (const [rel, files] of walk(root, (n) => !n.startsWith('.') && n !== 'node_modules')) {
- for (const f of files) {
- if (!f.endsWith('.md')) continue;
- const path = rel === '.' ? f : `${rel}/${f}`;
- let fm: Record<string, any>;
- try { fm = readPage(join(root, path)).fm; } catch { continue; }
- if (String(fm.type ?? '').trim() !== 'Collection') continue;
- const addr = path.replace(/\.md$/, '');
- const views = Array.isArray(fm.views) ? fm.views : [];
- out.push({
- addr,
- title: String(fm.title ?? addr.split('/').pop()),
- boards: views.filter((v: any) => v && v.layout === 'board').map((v: any) => String(v.name ?? v.id ?? '')),
- });
- }
- }
- return out;
- }
- /** The lines of one sidebar section (COLLECTIONS, BOARDS) without their counts. */
- function section(lines: string[], name: string): string[] {
- const at = lines.findIndex((l) => l.trim() === name);
- if (at < 0) return [];
- const rest: string[] = [];
- for (const l of lines.slice(at + 1)) {
- if (/^ [A-Z][A-Z ]+$/.test(l)) break;
- rest.push(l.replace(/\s+\d+$/, '').trim());
- }
- return rest.filter(Boolean);
- }
- const { bin, tried } = findBin(root);
- if (!bin) {
- console.log(`warning: the Shtukbase binary with the text interface was not found (looked in: ${tried.join(', ') || 'PATH'}); ${INSTALL_HINT}`);
- process.exit(0);
- }
- console.log(`binary: ${bin.path} (${bin.from}); ${bin.version}`);
- const node = await reachNode(bin, root, { ownNode: process.argv.includes('--own-node') });
- if (typeof node === 'string') {
- console.log(`warning: ${node}`);
- process.exit(0);
- }
- try {
- const r = front(bin, node, ['sidebar', '--space', root, '--width', '200'], root);
- const lines = render(r.out);
- if (r.status !== 0 || !lines.length) {
- console.log(`warning: the sidebar is empty or has an error: ${r.err.trim().split('\n')[0] ?? ''}`);
- process.exit(0);
- }
- const shown = section(lines, 'COLLECTIONS');
- const boards = section(lines, 'BOARDS');
- const colls = collections();
- const missing = colls.filter((c) => !shown.some((s) => s === c.title || s.endsWith(' ' + c.title)));
- const declared = colls.flatMap((c) => c.boards.map((b) => `${c.addr} → ${b}`));
- console.log(`the project is readable (${node.note}): the sidebar shows ${shown.length} of ${colls.length} collections, ${boards.length} of ${declared.length} boards`);
- for (const c of missing) console.log(`warning: the collection is not in the TUI sidebar: /${c.addr} ("${c.title}")`);
- if (declared.length > boards.length) {
- console.log(`warning: ${declared.length} boards (views with layout: board), but the BOARDS section has ${boards.length}; ` +
- `a board is visible through shtuk run tui shot board <collection>#<view id>`);
- }
- } finally {
- node.stop();
- }