#!/usr/bin/env node /** * Sync the root README's generated blocks. * * The root README is the storefront โ€” it is what every visitor to * github.com/ofri-peretz/eslint reads first. Before this script it was * hand-maintained, and it drifted: it advertised 18 plugins while 30 were * published, and three of the names it listed had since been renamed. * * Two blocks are now derived, never authored: * * * * * * * Inputs (read-only, both generated by apps/docs/scripts/sync-plugin-stats.ts): * - apps/docs/src/data/plugin-stats.json โ€” per-plugin rules/category/version * - apps/docs/src/data/interlace-numbers.json โ€” the cross-repo counts contract * * Usage: tsx scripts/sync-root-readme.ts [--check] * --check exits 1 if the README is out of date (CI gate). */ import * as fs from 'fs'; import * as path from 'path'; import * as prettier from 'prettier'; const ROOT_DIR = path.join(__dirname, '..'); const README = path.join(ROOT_DIR, 'README.md'); const STATS = path.join(ROOT_DIR, 'apps/docs/src/data/plugin-stats.json'); const NUMBERS = path.join( ROOT_DIR, 'apps/docs/src/data/interlace-numbers.json', ); const DOCS = 'https://eslint.interlace.tools'; const UTM = 'utm_source=github&utm_medium=referral&utm_campaign=monorepo'; interface Plugin { name: string; rules: number; description: string; category: string; version: string; published: boolean; } /** * Section order is deliberate: security first (the reason most people arrive), * then the framework/data adapters, then the quality half of the ecosystem. * Categories come from plugin-stats.json โ€” a new category shows up as its own * section rather than silently vanishing from the table. */ const SECTIONS: Array<[category: string, heading: string]> = [ ['security', '๐Ÿ”’ Security โ€” languages, platforms & SDKs'], ['framework', '๐Ÿ—๏ธ Security โ€” application frameworks'], ['architecture', '๐Ÿงญ Architecture'], ['quality', 'โœจ Code quality'], ['react', 'โš›๏ธ React'], ]; /** * The docs site has two plugin trees, not five: `security` covers the security * and framework categories, `quality` covers everything else. Deriving the path * from the category directly would 404 on architecture/ and react/. */ function docsSection(category: string): 'security' | 'quality' { return category === 'security' || category === 'framework' ? 'security' : 'quality'; } const CONTENT_DIR = path.join(ROOT_DIR, 'apps/docs/content/docs'); /** * Link to the docs site only when the page actually exists in the content tree, * otherwise fall back to the package README. Two live drifts made this * necessary: the four AI-SDK plugins have no docs page at all, and * `jwt-security` / `postgresql-security` were renamed as packages while their * content directories kept the old `plugin-jwt` / `plugin-pg` slugs. Linking * blind produced 404s on the repo's most-read page. */ function docsLink(p: Plugin, missingDocs: string[]): string { const slug = p.name.replace(/^eslint-plugin-/, ''); const section = docsSection(p.category); // `postgresql-security` was renamed from `pg`; its content directory wasn't. const RENAMED: Record = { 'postgresql-security': 'pg' }; const candidates = [ slug, slug.replace(/-security$/, ''), RENAMED[slug], ].filter(Boolean); for (const candidate of candidates) { const dir = path.join(CONTENT_DIR, section, `plugin-${candidate}`); if (fs.existsSync(dir)) { return `[docs](${DOCS}/docs/${section}/plugin-${candidate}?${UTM})`; } } missingDocs.push(p.name); return `[readme](./packages/${p.name})`; } /** First clause of the description โ€” the full sentence is too wide for a table cell. */ function shortDescription(description: string): string { const afterDash = description.split(' โ€” ')[1] ?? description; return afterDash.charAt(0).toUpperCase() + afterDash.slice(1); } function row(p: Plugin, missingDocs: string[]): string { const npm = `https://www.npmjs.com/package/${p.name}`; const dm = `https://img.shields.io/npm/dm/${p.name}?style=flat-square&label=`; return `| [\`${p.name}\`](${npm}) | ${p.rules} | ${shortDescription(p.description)} | ${docsLink(p, missingDocs)} | ![downloads](${dm}) |`; } function packageTable(plugins: Plugin[], missingDocs: string[]): string { const out: string[] = []; for (const [category, heading] of SECTIONS) { const inSection = plugins .filter((p) => p.category === category && p.published) .sort((a, b) => b.rules - a.rules || a.name.localeCompare(b.name)); if (inSection.length === 0) continue; const ruleTotal = inSection.reduce((n, p) => n + p.rules, 0); out.push( `### ${heading}`, '', `${inSection.length} plugin${inSection.length === 1 ? '' : 's'} ยท ${ruleTotal} rules`, '', '| Package | Rules | What it catches | Docs | Downloads |', '| :------ | ----: | :-------------- | :--- | :-------- |', ...inSection.map((p) => row(p, missingDocs)), '', ); } // Categories present in the data but absent from SECTIONS would otherwise be // dropped without a trace โ€” the exact failure mode this script exists to stop. const known = new Set(SECTIONS.map(([c]) => c)); const orphans = [ ...new Set( plugins .filter((p) => p.published && !known.has(p.category)) .map((p) => p.category), ), ]; if (orphans.length > 0) { throw new Error( `plugin-stats.json has categories with no README section: ${orphans.join(', ')} โ€” add them to SECTIONS in scripts/sync-root-readme.ts`, ); } return out.join('\n').trimEnd(); } function numbersLine(n: { plugins: { total: number; security: number; quality: number; react: number }; rules: { total: number; security: number; quality: number; react: number }; generatedAt: string; }): string { return [ `**${n.plugins.total} published plugins ยท ${n.rules.total} rules** โ€”`, `${n.plugins.security} security plugins (${n.rules.security} rules),`, `${n.plugins.quality} code-quality plugins (${n.rules.quality} rules),`, `${n.plugins.react} React plugins (${n.rules.react} rules).`, `Counts generated ${n.generatedAt} from the source tree โ€” never hand-typed.`, ].join(' '); } /** * The two count sources are generated by the same script but committed as * separate files, so one can be refreshed without the other. If they disagree, * the README would advertise a total that its own table contradicts โ€” fail * loudly instead, because a wrong number here is exactly the defect this * generator exists to prevent. */ function assertCountsAgree( plugins: Plugin[], n: { plugins: { total: number }; rules: { total: number } }, ): void { const published = plugins.filter((p) => p.published); const pluginTotal = published.length; const ruleTotal = published.reduce((sum, p) => sum + p.rules, 0); if (pluginTotal !== n.plugins.total || ruleTotal !== n.rules.total) { throw new Error( `interlace-numbers.json disagrees with plugin-stats.json โ€” ` + `manifest says ${n.plugins.total} plugins / ${n.rules.total} rules, ` + `the table sums to ${pluginTotal} / ${ruleTotal}. ` + `Re-run \`tsx apps/docs/scripts/sync-plugin-stats.ts\` and commit both files.`, ); } } function replaceBlock(source: string, name: string, body: string): string { const start = ``; const end = ``; const from = source.indexOf(start); const to = source.indexOf(end); if (from === -1 || to === -1 || to < from) { throw new Error(`README.md is missing the ${name} marker block`); } // A merge conflict can leave a duplicate marker pair. `indexOf` would then // splice between the first START and the first END and silently orphan the // second pair, so refuse rather than half-rewrite the file. if ( source.indexOf(start, from + 1) !== -1 || source.indexOf(end, to + 1) !== -1 ) { throw new Error( `README.md has more than one ${name} marker block โ€” resolve the duplicate before syncing`, ); } return ( source.slice(0, from + start.length) + `\n\n${body}\n\n` + source.slice(to) ); } async function main(): Promise { const check = process.argv.includes('--check'); const stats = JSON.parse(fs.readFileSync(STATS, 'utf-8')); const plugins: Plugin[] = Array.isArray(stats) ? stats : stats.plugins; const numbers = JSON.parse(fs.readFileSync(NUMBERS, 'utf-8')); assertCountsAgree(plugins, numbers); const missingDocs: string[] = []; const before = fs.readFileSync(README, 'utf-8'); let after = replaceBlock( before, 'PACKAGE_TABLE', packageTable(plugins, missingDocs), ); after = replaceBlock(after, 'NUMBERS', numbersLine(numbers)); // Prettier pads markdown table columns, so raw generator output and the // committed file never match byte-for-byte. Format here and `--check` stays // a real drift signal instead of a permanent red. const config = await prettier.resolveConfig(README); after = await prettier.format(after, { ...config, filepath: README, parser: 'markdown', }); if (missingDocs.length > 0) { console.warn( `โš ๏ธ ${missingDocs.length} plugin(s) have no docs page and fall back to their README: ${missingDocs.join(', ')}`, ); } if (after === before) { console.log('โœ… Root README is in sync.'); return; } if (check) { console.error( 'โŒ Root README is out of date. Run `npm run sync:root-readme` and commit the result.', ); process.exit(1); } fs.writeFileSync(README, after); console.log('โœ… Root README updated.'); } main().catch((err) => { console.error(err instanceof Error ? err.message : err); process.exit(1); });