ShtukbaseStandard
包文件→ .shtukbase/packages/tui/checks/doctor.ts

checks/doctor.ts

75 行, 3.5 KB.

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

  1. // Sensor (only watches; the check never fails): is the terminal front here, which version, does it read this project
  2. // (the sidebar is not empty), and what of the project it does not show yet: collections and saved boards
  3. // (`views` with `layout: board`) missing from the sidebar. Run: `shtuk run tui doctor [--own-node]`.
  4. import { join } from 'node:path';
  5. import { readPage, walk } from '../../collections/checks/lib/pages.ts';
  6. import { INSTALL_HINT, findBin, front, reachNode, render } from '../lib/tui.ts';
  7. const root = process.cwd();
  8. /** Collection pages of the project (front matter `type: Collection`), outside dot folders: title and board views. */
  9. function collections(): { addr: string; title: string; boards: string[] }[] {
  10. const out: { addr: string; title: string; boards: string[] }[] = [];
  11. for (const [rel, files] of walk(root, (n) => !n.startsWith('.') && n !== 'node_modules')) {
  12. for (const f of files) {
  13. if (!f.endsWith('.md')) continue;
  14. const path = rel === '.' ? f : `${rel}/${f}`;
  15. let fm: Record<string, any>;
  16. try { fm = readPage(join(root, path)).fm; } catch { continue; }
  17. if (String(fm.type ?? '').trim() !== 'Collection') continue;
  18. const addr = path.replace(/\.md$/, '');
  19. const views = Array.isArray(fm.views) ? fm.views : [];
  20. out.push({
  21. addr,
  22. title: String(fm.title ?? addr.split('/').pop()),
  23. boards: views.filter((v: any) => v && v.layout === 'board').map((v: any) => String(v.name ?? v.id ?? '')),
  24. });
  25. }
  26. }
  27. return out;
  28. }
  29. /** The lines of one sidebar section (COLLECTIONS, BOARDS) without their counts. */
  30. function section(lines: string[], name: string): string[] {
  31. const at = lines.findIndex((l) => l.trim() === name);
  32. if (at < 0) return [];
  33. const rest: string[] = [];
  34. for (const l of lines.slice(at + 1)) {
  35. if (/^ [A-Z][A-Z ]+$/.test(l)) break;
  36. rest.push(l.replace(/\s+\d+$/, '').trim());
  37. }
  38. return rest.filter(Boolean);
  39. }
  40. const { bin, tried } = findBin(root);
  41. if (!bin) {
  42. console.log(`warning: the Shtukbase binary with the text interface was not found (looked in: ${tried.join(', ') || 'PATH'}); ${INSTALL_HINT}`);
  43. process.exit(0);
  44. }
  45. console.log(`binary: ${bin.path} (${bin.from}); ${bin.version}`);
  46. const node = await reachNode(bin, root, { ownNode: process.argv.includes('--own-node') });
  47. if (typeof node === 'string') {
  48. console.log(`warning: ${node}`);
  49. process.exit(0);
  50. }
  51. try {
  52. const r = front(bin, node, ['sidebar', '--space', root, '--width', '200'], root);
  53. const lines = render(r.out);
  54. if (r.status !== 0 || !lines.length) {
  55. console.log(`warning: the sidebar is empty or has an error: ${r.err.trim().split('\n')[0] ?? ''}`);
  56. process.exit(0);
  57. }
  58. const shown = section(lines, 'COLLECTIONS');
  59. const boards = section(lines, 'BOARDS');
  60. const colls = collections();
  61. const missing = colls.filter((c) => !shown.some((s) => s === c.title || s.endsWith(' ' + c.title)));
  62. const declared = colls.flatMap((c) => c.boards.map((b) => `${c.addr} → ${b}`));
  63. console.log(`the project is readable (${node.note}): the sidebar shows ${shown.length} of ${colls.length} collections, ${boards.length} of ${declared.length} boards`);
  64. for (const c of missing) console.log(`warning: the collection is not in the TUI sidebar: /${c.addr} ("${c.title}")`);
  65. if (declared.length > boards.length) {
  66. console.log(`warning: ${declared.length} boards (views with layout: board), but the BOARDS section has ${boards.length}; ` +
  67. `a board is visible through shtuk run tui shot board <collection>#<view id>`);
  68. }
  69. } finally {
  70. node.stop();
  71. }