(
command: string,
args?: ReadonlyArray<string> | ExecFileOptions,
options: ExecFileOptions = { waitForClose: true }
)
| 70 | * @param options |
| 71 | */ |
| 72 | export function execFileAsync( |
| 73 | command: string, |
| 74 | args?: ReadonlyArray<string> | ExecFileOptions, |
| 75 | options: ExecFileOptions = { waitForClose: true } |
| 76 | ) { |
| 77 | // args is optional, so if it's not an array we interpret it as options |
| 78 | if (!Array.isArray(args)) { |
| 79 | options = { |
| 80 | ...options, |
| 81 | ...args, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (options.cwd) { |
| 86 | options.cwd = convertToOSPath(options.cwd); |
| 87 | } |
| 88 | |
| 89 | // The child process close event is emitted when the stdio streams |
| 90 | // have all terminated. If those streams are shared with other |
| 91 | // processes, that means we won't receive a 'close' until all processes |
| 92 | // have exited, so we may want to respond to 'exit' instead. |
| 93 | // (The downside of responding to 'exit' is that the streams may not be |
| 94 | // fully flushed, so we could miss captured output. Only use this |
| 95 | // option when needed.) |
| 96 | const exitEvent = options.waitForClose ? 'close' : 'exit'; |
| 97 | |
| 98 | return new Promise((resolve, reject) => { |
| 99 | let child: ReturnType<typeof child_process.exec>; |
| 100 | const spawnArgs: ReadonlyArray<string> = Array.isArray(args) ? args : []; |
| 101 | const { cwd, env, stdio } = options; |
| 102 | |
| 103 | if (process.platform !== 'win32') { |
| 104 | child = child_process.spawn(command, spawnArgs, { cwd, env, stdio }); |
| 105 | } else { |
| 106 | // https://github.com/nodejs/node-v0.x-archive/issues/2318 |
| 107 | spawnArgs.forEach(arg => { |
| 108 | command += ' ' + arg; |
| 109 | }); |
| 110 | child = child_process.exec(command, { cwd, env }); |
| 111 | } |
| 112 | |
| 113 | let capturedStdout = ''; |
| 114 | if (child.stdout) { |
| 115 | if (options.destination) { |
| 116 | child.stdout.pipe(options.destination); |
| 117 | } else { |
| 118 | child.stdout.setEncoding('utf8'); |
| 119 | child.stdout.on('data', (data: string) => { |
| 120 | capturedStdout += data; |
| 121 | }); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | let capturedStderr = ''; |
| 126 | if (child.stderr) { |
| 127 | child.stderr.setEncoding('utf8'); |
| 128 | child.stderr.on('data', (data: string) => { |
| 129 | capturedStderr += data; |
no test coverage detected
searching dependent graphs…