| 585 | } |
| 586 | |
| 587 | export async function runRemoteCommand(params: { output: Log; onDidInput?: Event<string>; stdin?: NodeJS.ReadStream; stdout?: NodeJS.WriteStream; stderr?: NodeJS.WriteStream }, { remoteExec, remotePtyExec }: ContainerProperties, cmd: string[], cwd?: string, options: { remoteEnv?: NodeJS.ProcessEnv; pty?: boolean; print?: 'off' | 'continuous' | 'end' } = {}) { |
| 588 | const print = options.print || 'end'; |
| 589 | let sub: Disposable | undefined; |
| 590 | let pp: Exec | PtyExec; |
| 591 | let cmdOutput = ''; |
| 592 | if (options.pty) { |
| 593 | const p = pp = await remotePtyExec({ |
| 594 | env: options.remoteEnv, |
| 595 | cwd, |
| 596 | cmd: cmd[0], |
| 597 | args: cmd.slice(1), |
| 598 | output: params.output, |
| 599 | }); |
| 600 | p.onData(chunk => { |
| 601 | cmdOutput += chunk; |
| 602 | if (print === 'continuous') { |
| 603 | if (params.stdout) { |
| 604 | params.stdout.write(chunk); |
| 605 | } else { |
| 606 | params.output.raw(chunk); |
| 607 | } |
| 608 | } |
| 609 | }); |
| 610 | if (p.write && params.onDidInput) { |
| 611 | params.onDidInput(data => p.write!(data)); |
| 612 | } else if (p.write && params.stdin) { |
| 613 | const listener = (data: Buffer): void => p.write!(data.toString()); |
| 614 | const stdin = params.stdin; |
| 615 | if (stdin.isTTY) { |
| 616 | stdin.setRawMode(true); |
| 617 | } |
| 618 | stdin.on('data', listener); |
| 619 | sub = { dispose: () => stdin.off('data', listener) }; |
| 620 | } |
| 621 | } else { |
| 622 | const p = pp = await remoteExec({ |
| 623 | env: options.remoteEnv, |
| 624 | cwd, |
| 625 | cmd: cmd[0], |
| 626 | args: cmd.slice(1), |
| 627 | output: params.output, |
| 628 | }); |
| 629 | const stdout: Buffer[] = []; |
| 630 | if (print === 'continuous' && params.stdout) { |
| 631 | p.stdout.pipe(params.stdout); |
| 632 | } else { |
| 633 | p.stdout.on('data', chunk => { |
| 634 | stdout.push(chunk); |
| 635 | if (print === 'continuous') { |
| 636 | params.output.raw(chunk.toString()); |
| 637 | } |
| 638 | }); |
| 639 | } |
| 640 | const stderr: Buffer[] = []; |
| 641 | if (print === 'continuous' && params.stderr) { |
| 642 | p.stderr.pipe(params.stderr); |
| 643 | } else { |
| 644 | p.stderr.on('data', chunk => { |