import { runScreenshot } from './screenshot' import { runScreenshots } from './screenshots' import { runSlides } from './slides' interface ScreenshotCommandOptions { port?: number verbose?: boolean } const VALUE_FLAGS = new Set([ '--sim', '--port', '-p', '--output', '-o', '--out', '--plan', '--app', '--device', '--org', '--repo', '--api-origin', '--branch', '--platform', '--slides', '--shell-origin', '--timeout', '--ids', '--out-dir', ]) const SUBCOMMANDS = new Set(['capture', 'slides', 'appstore', 'layers']) 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 removeAt(args: string[], index: number): string[] { return [...args.slice(0, index), ...args.slice(index + 1)] } function stripSimFlag(args: string[]): string[] { const out: string[] = [] for (let i = 0; i < args.length; i++) { if (args[i] === '--sim') { i++ continue } out.push(args[i]) } return out } export async function runScreenshotCommand( args: string[], opts: ScreenshotCommandOptions, ): Promise { const sub = findSubcommand(args) if (!sub || sub.name === 'capture') { await runScreenshot(sub ? removeAt(args, sub.index) : args, opts) return } if (sub.name === 'slides') { await runSlides(removeAt(args, sub.index), opts) return } if (sub.name === 'layers') { const { runScreenshotLayers } = await import('./screenshot-layers') await runScreenshotLayers(removeAt(args, sub.index), opts) return } const appstoreArgs = removeAt(args, sub.index) const nested = findSubcommand(appstoreArgs) if (nested?.name === 'capture') { // repo-only tooling: screenshots-capture statically imports playwright-core, // which the published CLI never ships (it's external in build-cli.ts). a // static import here would put that bare import at the top of THIS chunk, // so every published `rnx screenshot` printed ERR_MODULE_NOT_FOUND — // keep it a runtime dynamic import that only evaluates when capture runs. try { const { runScreenshotsCapture } = await import('./screenshots-capture') return await runScreenshotsCapture( stripSimFlag(removeAt(appstoreArgs, nested.index)), ) } catch (err) { if (err instanceof Error && 'code' in err && err.code === 'ERR_MODULE_NOT_FOUND') { console.error( 'screenshot appstore capture is repo-only tooling — it needs the rnx workspace (playwright is not shipped with the published CLI).', ) return 1 } throw err } } return runScreenshots(appstoreArgs, opts) }