import type { DoctorReport, DoctorSeverity } from './types.js' const MARK: Record = { error: '✗', info: 'i', warning: '!' } const COLOR: Record = { error: '\x1b[31m', info: '\x1b[36m', warning: '\x1b[33m' } const summaryLine = (r: DoctorReport) => `${r.summary.error} error(s), ${r.summary.warning} warning(s), ${r.summary.info} info${r.summary.silenced ? ` (${r.summary.silenced} silenced)` : ''}` /** Human-readable report: header, findings worst first with fix and doc link, summary. */ export function formatReport(report: DoctorReport, { color = false }: { color?: boolean } = {}): string { const c = (sev: DoctorSeverity, text: string) => (color ? `${COLOR[sev]}${text}\x1b[0m` : text) const lines = [`ipal-doctor ${report.rulesVersion} — ${report.site.path}`, `site: ${report.site.domain ?? '(no domain)'} · ipal-kit ${report.site.plugin ?? 'not installed'}`, ''] const active = report.findings.filter((f) => !f.silenced) if (!active.length) {lines.push('No findings.')} for (const f of active) { lines.push(c(f.severity, `${MARK[f.severity]} [${f.severity}] ${f.rule}: ${f.message}`)) if (f.file) {lines.push(` file: ${f.file}`)} if (f.fix) {lines.push(` fix: ${f.fix}`)} if (f.docs) {lines.push(` docs: node_modules/@intecion/ipal-kit/${f.docs}`)} } const silenced = report.findings.filter((f) => f.silenced) if (silenced.length) { lines.push('', 'Silenced:') for (const f of silenced) {lines.push(` - ${f.rule}: ${f.message} — ${f.reason}`)} } if (report.skipped.length) {lines.push('', `Not run: ${report.skipped.join(', ')} (--url, --deep, or without --offline)`)} lines.push('', `ipal-doctor: ${summaryLine(report)}`) return lines.join('\n') } const cell = (report: DoctorReport, rule: string) => { const hits = report.findings.filter((f) => !f.silenced && (f.rule === rule || f.rule.startsWith(`${rule}:`))) if (!hits.length) {return report.findings.some((f) => f.silenced && f.rule === rule) ? 'silenced' : '✅'} const worst = hits.some((f) => f.severity === 'error') ? '❌' : hits.some((f) => f.severity === 'warning') ? '⚠️' : 'ℹ️' return `${worst} ${hits.length > 1 ? hits.length : ''}`.trim() } /** One Markdown table: rule × site (the drift sweep, `--sites … --md`). */ export function formatDriftTable(reports: DoctorReport[]): string { const name = (r: DoctorReport) => r.site.path.split('/').filter(Boolean).at(-1) ?? r.site.path const rules = [...new Set(reports.flatMap((r) => r.findings.map((f) => f.rule.split(':')[0])))].sort() const head = `| Rule | ${reports.map(name).join(' | ')} |` const sep = `|---|${reports.map(() => '---').join('|')}|` const totals = `| **errors / warnings / info** | ${reports.map((r) => `${r.summary.error} / ${r.summary.warning} / ${r.summary.info}`).join(' | ')} |` return [head, sep, totals, ...rules.map((rule) => `| \`${rule}\` | ${reports.map((r) => cell(r, rule)).join(' | ')} |`)].join('\n') }