|
| 1 | +import { runCmd, runCmdSync, type ExecResult } from '../../src/utils/exec.ts'; |
| 2 | + |
| 3 | +const CLI_TIMEOUT_MS = 120_000; |
| 4 | + |
| 5 | +export type CliJsonResult = { |
| 6 | + status: number; |
| 7 | + json?: any; |
| 8 | + stdout: string; |
| 9 | + stderr: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export function runSourceCliJsonSync( |
| 13 | + args: string[], |
| 14 | + options?: { env?: NodeJS.ProcessEnv }, |
| 15 | +): CliJsonResult { |
| 16 | + const result = runCmdSync( |
| 17 | + process.execPath, |
| 18 | + ['--experimental-strip-types', 'src/bin.ts', ...args], |
| 19 | + { |
| 20 | + allowFailure: true, |
| 21 | + env: options?.env, |
| 22 | + timeoutMs: CLI_TIMEOUT_MS, |
| 23 | + }, |
| 24 | + ); |
| 25 | + return cliJsonResult(result); |
| 26 | +} |
| 27 | + |
| 28 | +export async function runBuiltCliJson( |
| 29 | + args: string[], |
| 30 | + env: NodeJS.ProcessEnv, |
| 31 | +): Promise<CliJsonResult> { |
| 32 | + const result = await runCmd(process.execPath, ['bin/agent-device.mjs', ...args], { |
| 33 | + allowFailure: true, |
| 34 | + env, |
| 35 | + timeoutMs: CLI_TIMEOUT_MS, |
| 36 | + }); |
| 37 | + return cliJsonResult(result); |
| 38 | +} |
| 39 | + |
| 40 | +export function formatResultDebug(step: string, args: string[], result: CliJsonResult): string { |
| 41 | + const jsonText = |
| 42 | + result.json === undefined ? '(unparseable)' : JSON.stringify(result.json, null, 2); |
| 43 | + return [ |
| 44 | + `step: ${step}`, |
| 45 | + `command: agent-device ${args.join(' ')}`, |
| 46 | + `status: ${result.status}`, |
| 47 | + `stderr:`, |
| 48 | + result.stderr || '(empty)', |
| 49 | + `stdout:`, |
| 50 | + result.stdout || '(empty)', |
| 51 | + `json:`, |
| 52 | + jsonText, |
| 53 | + ].join('\n'); |
| 54 | +} |
| 55 | + |
| 56 | +function cliJsonResult(result: ExecResult): CliJsonResult { |
| 57 | + let json: any; |
| 58 | + try { |
| 59 | + json = JSON.parse(result.stdout ?? ''); |
| 60 | + } catch { |
| 61 | + json = undefined; |
| 62 | + } |
| 63 | + return { |
| 64 | + status: result.exitCode, |
| 65 | + json, |
| 66 | + stdout: json ? '<JSON output>' : (result.stdout ?? ''), |
| 67 | + stderr: result.stderr ?? '', |
| 68 | + }; |
| 69 | +} |
0 commit comments