(
command: string,
opts?: ProcessOptions,
)
| 176 | } |
| 177 | |
| 178 | private async exec( |
| 179 | command: string, |
| 180 | opts?: ProcessOptions, |
| 181 | ): Promise<ExecResult> { |
| 182 | const exec = await this.container.exec({ |
| 183 | Cmd: ['sh', '-c', command], |
| 184 | AttachStdout: true, |
| 185 | AttachStderr: true, |
| 186 | WorkingDir: opts?.cwd ? this.abs(opts.cwd) : this.workdir, |
| 187 | Env: this.envArray(opts?.env), |
| 188 | }) |
| 189 | const stream = await exec.start({ hijack: true, stdin: false }) |
| 190 | |
| 191 | const stdoutChunks: Array<Buffer> = [] |
| 192 | const stderrChunks: Array<Buffer> = [] |
| 193 | const outW = new Writable({ |
| 194 | write(chunk, _enc, cb) { |
| 195 | stdoutChunks.push(chunk as Buffer) |
| 196 | cb() |
| 197 | }, |
| 198 | }) |
| 199 | const errW = new Writable({ |
| 200 | write(chunk, _enc, cb) { |
| 201 | stderrChunks.push(chunk as Buffer) |
| 202 | cb() |
| 203 | }, |
| 204 | }) |
| 205 | this.docker.modem.demuxStream(stream, outW, errW) |
| 206 | |
| 207 | if (opts?.signal) { |
| 208 | opts.signal.addEventListener('abort', () => stream.destroy(), { |
| 209 | once: true, |
| 210 | }) |
| 211 | } |
| 212 | |
| 213 | await new Promise<void>((resolve, reject) => { |
| 214 | stream.on('end', resolve) |
| 215 | stream.on('error', reject) |
| 216 | }) |
| 217 | |
| 218 | const info = await exec.inspect() |
| 219 | return { |
| 220 | stdout: Buffer.concat(stdoutChunks).toString('utf8'), |
| 221 | stderr: Buffer.concat(stderrChunks).toString('utf8'), |
| 222 | exitCode: info.ExitCode ?? 0, |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private async spawnProcess( |
| 227 | command: string, |
no test coverage detected