(tree: CommandTree, argv: string[])
| 59 | } |
| 60 | |
| 61 | async function captureRun(tree: CommandTree, argv: string[]): Promise<Captured> { |
| 62 | const captured: Captured = { stdout: '', stderr: '', exit: undefined } |
| 63 | const origStdout = process.stdout.write.bind(process.stdout) |
| 64 | const origStderr = process.stderr.write.bind(process.stderr) |
| 65 | const origExit = process.exit.bind(process) |
| 66 | |
| 67 | process.stdout.write = ((chunk: string | Uint8Array) => { |
| 68 | captured.stdout += typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk) |
| 69 | return true |
| 70 | }) as typeof process.stdout.write |
| 71 | |
| 72 | process.stderr.write = ((chunk: string | Uint8Array) => { |
| 73 | captured.stderr += typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk) |
| 74 | return true |
| 75 | }) as typeof process.stderr.write |
| 76 | |
| 77 | process.exit = ((code?: number) => { |
| 78 | captured.exit = code |
| 79 | // do not throw; the catch block should `return` after exit |
| 80 | }) as typeof process.exit |
| 81 | |
| 82 | try { |
| 83 | await run(tree, argv) |
| 84 | } |
| 85 | finally { |
| 86 | process.stdout.write = origStdout |
| 87 | process.stderr.write = origStderr |
| 88 | process.exit = origExit |
| 89 | } |
| 90 | return captured |
| 91 | } |
| 92 | |
| 93 | function makeTree(cmd: CommandConstructor): CommandTree { |
| 94 | return { cmd: { command: cmd, subcommands: {} } } |
no test coverage detected