#!/usr/bin/env node // Guards the atomic library: every component file exports exactly one // component. Biome has no rule for this, so this runs in `npm run lint`. import { readdirSync, readFileSync, statSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; const root = fileURLToPath(new URL('../src/components/', import.meta.url)); const offenders = []; /** @param {string} dir */ function walk(dir) { for (const name of readdirSync(dir)) { const path = `${dir}/${name}`; if (statSync(path).isDirectory()) { walk(path); } else if (name.endsWith('.jsx')) { const source = readFileSync(path, 'utf8'); const components = [ ...source.matchAll(/^export\s+function\s+([A-Z]\w*)/gm), ].map((match) => match[1]); if (components.length > 1) { offenders.push(`${path}: ${components.join(', ')}`); } } } } walk(root); if (offenders.length) { console.error('Component files exporting more than one component:'); for (const line of offenders) console.error(` ${line}`); process.exit(1); } console.log('one-component-per-file: ok');