import { runCpuProfile } from './cpu-profile' import { runInspect } from './inspect' import { runReact } from './react' interface PerfOptions { port?: number verbose?: boolean } const VALUE_FLAGS = new Set(['--sim', '--port', '-p']) const SUBCOMMANDS = new Set(['cpu', 'react', 'shell', 'scroll']) function findSubcommand(args: string[]): { name: string; index: number } | null { for (let i = 0; i < args.length; i++) { const arg = args[i] if (arg.startsWith('-')) { if (VALUE_FLAGS.has(arg)) i++ continue } return SUBCOMMANDS.has(arg) ? { name: arg, index: i } : null } return null } function withoutSubcommand(args: string[], index: number): string[] { return [...args.slice(0, index), ...args.slice(index + 1)] } function printHelp(): void { console.log(` rnx perf — performance profiling tools usage: rnx perf cpu [options] rnx perf react [options] rnx perf shell [options] rnx perf scroll [options] examples: rnx perf cpu --duration 5 --output /tmp/trace.cpuprofile # writes page + every worker profile and a role-labelled manifest rnx perf react summary --limit 10 rnx perf shell start rnx perf shell stop --json rnx perf scroll start rnx perf scroll stop --json `) } export async function runPerf(args: string[], opts: PerfOptions): Promise { const sub = findSubcommand(args) if (!sub) { printHelp() return args.includes('--help') || args.includes('-h') ? 0 : 1 } const rest = withoutSubcommand(args, sub.index) if (sub.name === 'cpu') return runCpuProfile(rest, opts) if (sub.name === 'react') return runReact(rest, opts) const inspectArgs = rest.includes('--help') || rest.includes('-h') ? [] : rest await runInspect(['debug', 'perf', ...inspectArgs], { ...opts, internalPerfCommand: sub.name === 'scroll' ? 'scroll' : 'shell', }) return typeof process.exitCode === 'number' ? process.exitCode : 0 }