// how this CLI re-invokes itself as a child process. // // two shapes exist and only the build decides which one is running: // // standalone the bun-compiled executable IS the entry point. it takes the // CLI argv directly and there is no script to hand it. // entry script every other shape (dev checkout, the repository dist-cli // bundle) runs through the javascript runtime executing now, // with the script as its first argument. // // a presence check cannot tell them apart. inside the compiled binary // `process.argv[1]` is a path in bun's virtual filesystem (`/$bunfs/root/...`) // that passes `existsSync` in-process, so every site that probed argv[1] // re-invoked itself as `rnx ` and the child died on // `unknown command`. the build-time constant is the only honest signal. import { existsSync } from 'node:fs' import { basename, dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { rnxPublicBrand } from '../src/public-brand.ts' import { IS_STANDALONE } from './standalone.ts' export interface RnxSelfInvocation { /** absolute path to spawn. */ executable: string /** arguments that precede the CLI's own argv. */ prefixArgs: string[] } function sourceEntryPath(): string | null { try { const candidate = join(dirname(fileURLToPath(import.meta.url)), 'bin.ts') return existsSync(candidate) ? candidate : null } catch { return null } } function isCliEntry(path: string): boolean { const name = basename(path) return name === 'bin.ts' || name === 'bin.js' } export function rnxSelfInvocation(): RnxSelfInvocation { if (IS_STANDALONE) return { executable: process.execPath, prefixArgs: [] } const entry = process.argv[1] if (entry && isCliEntry(entry)) { return { executable: process.execPath, prefixArgs: [entry] } } const sourceEntry = sourceEntryPath() if (sourceEntry) { return { executable: process.execPath, prefixArgs: [sourceEntry] } } if (!entry) { throw new Error(`could not locate the ${rnxPublicBrand.commandName} entry script`) } return { executable: process.execPath, prefixArgs: [entry] } }