#!/usr/bin/env bare
import process from 'process'
import { scanStream } from '../lib/scan-stream.js'
import { TreeReducer } from '../lib/reducers/tree-reducer.js'
import { MetricsReducer } from '../lib/reducers/metrics-reducer.js'
import { buildIgnoreMatcher } from '../lib/ignore.js'
const rootDir = process.argv[2] || '.'
const maxDepth = parseInt(process.argv[3] || '2', 10)
const extraIgnores = (process.argv[4] || '').split(',').filter(Boolean)
const shallow = process.argv.includes('--shallow')
console.log('Profile-scan:', rootDir)
console.log(' maxDepth:', maxDepth)
console.log(' shallow: ', shallow)
console.log(' ignore: ', extraIgnores.length ? extraIgnores.join(',') : '(builtins only)')
console.log()
const ignore = buildIgnoreMatcher(rootDir, extraIgnores)
const source = scanStream(rootDir, { maxDepth, ignore, shallow })
const tree = new TreeReducer().attach(source)
const metrics = new MetricsReducer().attach(source)
await new Promise((resolveP, reject) => {
source.once('scan-end', resolveP)
source.once('error', reject)
})
const graph = tree.finalize()
const m = metrics.report()
console.log('=== Results ===')
console.log(` total duration: ${m.totalDuration}ms`)
console.log(` modules: ${m.moduleCount}`)
console.log(` files discovered: ${m.fileCount}`)
console.log(` files parsed: ${m.parseCount}`)
console.log(` parse errors: ${m.parseErrors}`)
console.log(` import edges: ${m.importEdges} (${m.implicitEdges} implicit)`)
console.log(` bytes read: ${(m.bytesRead / 1024 / 1024).toFixed(2)} MB`)
console.log(` largest file: ${m.largestFile.path} (${(m.largestFile.size / 1024).toFixed(0)} KB)`)
console.log()
console.log(` throughput: ${m.throughputFilesPerSec} files/s, ${m.throughputMBPerSec} MB/s`)
console.log(` parse time p50: ${m.parseTimeP50}ms`)
console.log(` parse time p95: ${m.parseTimeP95}ms`)
console.log(` parse time p99: ${m.parseTimeP99}ms`)
console.log(` parse time total: ${m.parseTimeTotal}ms`)
console.log()
console.log('=== File extensions ===')
for (const [ext, count] of m.topExtensions) {
console.log(` ${ext.padEnd(14)} ${count}`)
}
console.log()
console.log('=== Slowest modules ===')
for (const mod of m.slowestModules) {
console.log(` ${mod.name.padEnd(30)} ${mod.duration}ms`)
}