import { Marked } from 'marked'; import { markedTerminal } from 'marked-terminal'; import Table from 'cli-table3'; import type { AuditResult } from './audit.ts'; import { shownLevel } from './scoring.ts'; import type { DoctorReport, CheckStatus } from './doctor.ts'; const ANSI = { bold: (s: string): string => `\x1b[1m${s}\x1b[22m`, dim: (s: string): string => `\x1b[2m${s}\x1b[22m`, green: (s: string): string => `\x1b[32m${s}\x1b[39m`, yellow: (s: string): string => `\x1b[33m${s}\x1b[39m`, red: (s: string): string => `\x1b[31m${s}\x1b[39m`, }; let terminalMarked: Marked | null = null; function getTerminalMarked(): Marked { if (terminalMarked) return terminalMarked; const m = new Marked(); m.use(markedTerminal({ reflowText: false, tab: 2 }) as never); terminalMarked = m; return m; } const SYMBOL: Record = { aligned: 'โœ…', gap: 'โš ๏ธ ', over: '๐Ÿ”ต', excluded: 'โž–', }; export function reportMarkdown(r: AuditResult): string { const lines: string[] = []; lines.push(`# vdx audit report`); lines.push(''); lines.push(`- **Baseline**: \`${r.baseline}\``); lines.push(`- **Stack**: \`${r.stack}\``); lines.push(`- **Achieved level**: **${r.achieved_level}**`); lines.push(''); lines.push(`## Axes`); lines.push(''); lines.push(`| Axis | Class | Achieved | Target | Drift |`); lines.push(`|------|:-----:|:--------:|:------:|:------|`); for (const a of r.per_axis) { const cls = a.class === 'critical' ? '**C**' : 's'; const drift = a.suppressed ? 'โ€” suppressed โ€”' : `${SYMBOL[a.drift_kind] ?? ''} ${a.drift_kind}`; lines.push(`| \`${a.axis_id}\` | ${cls} | ${achievedCell(a)} | ${a.target} | ${drift} |`); } const notMet = r.per_axis.filter((a) => a.not_met && !a.suppressed && a.drift_kind !== 'excluded'); if (notMet.length > 0) { lines.push(''); lines.push(`## Not met`); lines.push(''); for (const a of notMet) lines.push(`- \`${a.axis_id}\` ${a.not_met!.level}: ${a.not_met!.reason}`); } const unknown = r.per_axis.filter((a) => a.unknown); if (unknown.length > 0) { lines.push(''); lines.push(`## Not checked`); lines.push(''); lines.push(`Counted as not met; the level stops below.`); lines.push(''); for (const a of unknown) lines.push(`- \`${a.axis_id}\` ${a.unknown!.level}: ${a.unknown!.reason}`); } const notes = r.per_axis.filter((a) => a.note); if (notes.length > 0) { lines.push(''); lines.push(`## In this clone`); lines.push(''); lines.push(`Not part of the level: the same repository scores the same in every clone.`); lines.push(''); for (const a of notes) lines.push(`- \`${a.axis_id}\`: ${a.note}`); } if (r.overrides.length > 0) { lines.push(''); lines.push(`## Overrides`); for (const o of r.overrides) { const kind = o.suppress ? 'suppress' : `target=${o.target}`; const until = o.until ? `, until ${o.until}` : ''; lines.push(`- \`${o.axis}\` โ€” ${kind}, reason: ${o.reason}${until}`); } } if (r.expired_overrides.length > 0) { lines.push(''); lines.push(`## โš ๏ธ Expired overrides`); for (const x of r.expired_overrides) lines.push(`- \`${x}\``); } return lines.join('\n') + '\n'; } /** `L1`, `โ€” (L3 ?)` for an axis that asks nothing below an unchecked L3, `L1 ยท L2 ?`, `L2 โš‘` with a clone note. */ function achievedCell(a: AuditResult['per_axis'][number]): string { if (a.suppressed || a.drift_kind === 'excluded') return a.achieved; const shown = shownLevel(a); let cell = shown ?? 'โ€”'; if (a.unknown) cell += shown ? ` ยท ${a.unknown.level} ?` : ` (${a.unknown.level} ?)`; if (a.note) cell += ' โš‘'; return cell; } export function reportJson(r: AuditResult): string { return JSON.stringify(r, null, 2); } export function reportAnsi(r: AuditResult): string { const md = reportMarkdown(r); const out = getTerminalMarked().parse(md) as string; return out.endsWith('\n') ? out : out + '\n'; } const CHECK_SYMBOL: Record = { ok: 'โœ…', warning: 'โš ๏ธ ', missing: 'โŒ', }; export function reportDoctorMarkdown(r: DoctorReport): string { const lines: string[] = []; lines.push('# vdx doctor report'); lines.push(''); lines.push(`**OK**: ${r.ok} **Warning**: ${r.warning} **Missing**: ${r.missing}`); lines.push(''); lines.push('| Check | Status | Detail | Remedy |'); lines.push('|-------|:------:|--------|--------|'); for (const c of r.checks) { const sym = CHECK_SYMBOL[c.status]; const level = c.level !== undefined ? ` (L${c.level})` : ''; const remedy = c.remedy ? `\`${c.remedy}\`` : 'โ€”'; lines.push(`| **${c.label}** | ${sym} ${c.status}${level} | ${c.message} | ${remedy} |`); } return lines.join('\n') + '\n'; } export function reportDoctorJson(r: DoctorReport): string { return JSON.stringify(r, null, 2); } const STATUS_RENDER: Record string> = { ok: ANSI.green, warning: ANSI.yellow, missing: ANSI.red, }; function termWidth(fallback = 100): number { const w = process.stdout.columns; if (typeof w === 'number' && w >= 60) return w; return fallback; } export function reportDoctorAnsi(r: DoctorReport): string { const lines: string[] = []; lines.push(ANSI.bold('vdx doctor')); const summary = `${STATUS_RENDER.ok(`โœ… ${r.ok} ok`)} ` + `${STATUS_RENDER.warning(`โš  ${r.warning} warning`)} ` + `${STATUS_RENDER.missing(`โœ— ${r.missing} missing`)}`; lines.push(summary); lines.push(''); const total = termWidth(); const checkW = 18; const statusW = 14; const remedyW = Math.max(20, Math.min(36, Math.floor((total - checkW - statusW - 6) * 0.4))); const detailW = Math.max(24, total - checkW - statusW - remedyW - 6); const table = new Table({ head: [ANSI.bold('Check'), ANSI.bold('Status'), ANSI.bold('Detail'), ANSI.bold('Remedy')], colWidths: [checkW, statusW, detailW, remedyW], wordWrap: true, wrapOnWordBoundary: true, style: { head: [], border: [] }, }); for (const c of r.checks) { const sym = CHECK_SYMBOL[c.status]; const level = c.level !== undefined ? ` L${c.level}` : ''; const statusCell = STATUS_RENDER[c.status](`${sym} ${c.status}${level}`); table.push([ANSI.bold(c.label), statusCell, c.message, c.remedy ?? ANSI.dim('โ€”')]); } lines.push(table.toString()); lines.push(''); return lines.join('\n'); }