// Collects every package's vitest coverage report into coverage/site, with an index page. // Expects `pnpm test -- --coverage --coverage.reporter=html --coverage.reporter=json-summary` // to have run first, so each package has coverage/index.html and coverage/coverage-summary.json. import fs from 'node:fs'; import path from 'node:path'; const root = process.cwd(); const outDir = path.join(root, 'coverage', 'site'); const metrics = ['lines', 'statements', 'functions', 'branches']; function findReports(dir, depth = 0) { const reports = []; for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { if (!entry.isDirectory() || entry.name === 'node_modules') { continue; } const full = path.join(dir, entry.name); if (entry.name === 'coverage' && fs.existsSync(path.join(full, 'coverage-summary.json'))) { reports.push(full); } else if (depth < 3) { reports.push(...findReports(full, depth + 1)); } } return reports; } function pct(covered, total) { return total === 0 ? 100 : (covered / total) * 100; } function grade(value) { return value >= 80 ? 'high' : value >= 50 ? 'medium' : 'low'; } const escapeHtml = (text) => String(text).replace( /[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c] ); fs.rmSync(outDir, { recursive: true, force: true }); fs.mkdirSync(outDir, { recursive: true }); const packages = findReports(path.join(root, 'packages')) .map((dir) => { const pkg = JSON.parse(fs.readFileSync(path.join(dir, '..', 'package.json'), 'utf8')); const summary = JSON.parse(fs.readFileSync(path.join(dir, 'coverage-summary.json'), 'utf8')); const slug = pkg.name.replace(/^@/, '').replace(/\//g, '-'); fs.cpSync(dir, path.join(outDir, slug), { recursive: true }); return { name: pkg.name, slug, total: summary.total }; }) .sort((a, b) => a.name.localeCompare(b.name)); if (packages.length === 0) { console.error('no coverage reports found under packages/; run the tests with --coverage first'); process.exit(1); } const totals = Object.fromEntries( metrics.map((metric) => [ metric, packages.reduce( (sum, { total }) => ({ covered: sum.covered + total[metric].covered, total: sum.total + total[metric].total, }), { covered: 0, total: 0 } ), ]) ); const cell = (value) => `
master at ${escapeHtml(sha)}, ${new Date().toISOString().slice(0, 10)}. Each row links to the package report.
| Package | ${metrics.map((m) => `${m} | `).join('')}
|---|