( command: string, cwd: string | undefined, stdioMode: StdioOptions, quiet: boolean, rejectOnFailure: boolean, )
| 20 | } |
| 21 | |
| 22 | export function spawnCliCommand( |
| 23 | command: string, |
| 24 | cwd: string | undefined, |
| 25 | stdioMode: StdioOptions, |
| 26 | quiet: boolean, |
| 27 | rejectOnFailure: boolean, |
| 28 | ): Promise<CommandResult> { |
| 29 | return new Promise((resolve, reject) => { |
| 30 | const child = spawn(command, { |
| 31 | stdio: stdioMode, |
| 32 | shell: getShell(), |
| 33 | cwd: cwd, |
| 34 | env: { ...process.env, FORCE_COLOR: '1' }, |
| 35 | }); |
| 36 | |
| 37 | // When using mode = 'inherit', the following output and error capture is a no-op |
| 38 | |
| 39 | let stdout = ''; |
| 40 | let stderr = ''; |
| 41 | |
| 42 | child.stdout?.on('data', (data: Buffer) => { |
| 43 | stdout += data.toString(); |
| 44 | if (!quiet) { |
| 45 | process.stdout.write(stdout); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | child.stderr?.on('data', (data: Buffer) => { |
| 50 | stderr += data.toString(); |
| 51 | if (!quiet) { |
| 52 | process.stderr.write(stderr); |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | child.on('close', code => { |
| 57 | if (code !== 0 && rejectOnFailure) { |
| 58 | reject(new Error(stderr.trim())); |
| 59 | } else { |
| 60 | resolve({ returnCode: code ?? 0, stdout, stderr }); |
| 61 | } |
| 62 | }); |
| 63 | |
| 64 | child.on('error', err => { |
| 65 | reject(err); |
| 66 | }); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | export async function getUserChoice<T>( |
| 71 | choices: Array<CliChoice<T> | Separator>, |
no test coverage detected