import fs from 'node:fs/promises'; import { existsSync } from 'node:fs'; import path from 'node:path'; const args = process.argv.slice(2); const defaultBareRoot = [ '/Volumes/storage/dev/pearcli/holepunch-repos/holepunchto_repos', '/Users/raven/dev/pearcli/holepunch-repos/holepunchto_repos' ].find((candidate) => existsSync(candidate)); const root = args.includes('--root') ? args[args.indexOf('--root') + 1] : defaultBareRoot; const out = args.includes('--out') ? args[args.indexOf('--out') + 1] : path.join(process.cwd(), 'artifacts', 'analysis'); if (!root || !out) { console.error('Usage: node scripts/analyze-bare-surface.mjs [--root ] [--out ]'); process.exit(1); } async function getRepoInfo(repoPath) { const pkgPath = path.join(repoPath, 'package.json'); try { const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf8')); return { repoPath, name: pkg.name || path.basename(repoPath), version: pkg.version || null, description: pkg.description || null, dependencies: Object.keys(pkg.dependencies || {}), optionalDependencies: Object.keys(pkg.optionalDependencies || {}), peerDependencies: Object.keys(pkg.peerDependencies || {}), exports: pkg.exports || null }; } catch { return null; } } const entries = await fs.readdir(root, { withFileTypes: true }); const repos = []; for (const entry of entries) { if (!entry.isDirectory()) continue; if (!entry.name.startsWith('bare-') && entry.name !== 'node-bare-bundle') continue; repos.push(path.join(root, entry.name)); } const analyzed = (await Promise.all(repos.map(getRepoInfo))).filter(Boolean); const grouped = { runtime: analyzed.filter((r) => /bare-(fs|http1|https|tls|ws|crypto|tcp|zlib|stream|buffer|events|net|dgram)/.test(r.name)), loaderAndBundling: analyzed.filter((r) => /bare-(module|pack|module-resolve|module-traverse|module-lexer)/.test(r.name)), wrappers: analyzed.filter((r) => /bare-node/.test(r.name)), other: analyzed.filter((r) => !/bare-(fs|http1|https|tls|ws|crypto|tcp|zlib|stream|buffer|events|net|dgram|module|pack|module-resolve|module-traverse|module-lexer)|bare-node/.test(r.name)) }; await fs.mkdir(out, { recursive: true }); await fs.writeFile( path.join(out, 'bare-surface.json'), JSON.stringify( { scannedRoot: root, generatedAt: new Date().toISOString(), repositories: analyzed.sort((a, b) => a.name.localeCompare(b.name)), grouped }, null, 2 ) ); console.log('Wrote', path.join(out, 'bare-surface.json'));