| 19 | export const EOT = '\u2404'; |
| 20 | |
| 21 | export async function launch(remoteExec: ExecFunction | Exec, output: Log, agentSessionId?: string, platform: NodeJS.Platform = 'linux', hostName: 'Host' | 'Container' = 'Container'): Promise<ShellServer> { |
| 22 | const isExecFunction = typeof remoteExec === 'function'; |
| 23 | const isWindows = platform === 'win32'; |
| 24 | const p = isExecFunction ? await remoteExec({ |
| 25 | env: agentSessionId ? { VSCODE_REMOTE_CONTAINERS_SESSION: agentSessionId } : {}, |
| 26 | cmd: isWindows ? 'powershell' : '/bin/sh', |
| 27 | args: isWindows ? ['-NoProfile', '-Command', '-'] : [], |
| 28 | output, |
| 29 | }) : remoteExec; |
| 30 | if (!isExecFunction) { |
| 31 | // TODO: Pass in agentSessionId. |
| 32 | const stdinText = isWindows |
| 33 | ? `powershell -NoProfile -Command "powershell -NoProfile -Command -"\n` // Nested PowerShell (for some reason) avoids the echo of stdin on stdout. |
| 34 | : `/bin/sh -c 'echo ${EOT}; /bin/sh'\n`; |
| 35 | p.stdin.write(stdinText); |
| 36 | const eot = new Promise<void>(resolve => { |
| 37 | let stdout = ''; |
| 38 | const stdoutDecoder = new StringDecoder(); |
| 39 | p.stdout.on('data', function eotListener(chunk: Buffer) { |
| 40 | stdout += stdoutDecoder.write(chunk); |
| 41 | if (stdout.includes(stdinText)) { |
| 42 | p.stdout.off('data', eotListener); |
| 43 | resolve(); |
| 44 | } |
| 45 | }); |
| 46 | }); |
| 47 | await eot; |
| 48 | } |
| 49 | |
| 50 | const monitor = monitorProcess(p); |
| 51 | |
| 52 | let lastExec: Promise<any> | undefined; |
| 53 | async function exec(cmd: PlatformSwitch<string>, options?: { logLevel?: LogLevel; logOutput?: boolean | 'continuous' | 'silent'; stdin?: Buffer }) { |
| 54 | const currentExec = lastExec = (async () => { |
| 55 | try { |
| 56 | await lastExec; |
| 57 | } catch (err) { |
| 58 | // ignore |
| 59 | } |
| 60 | return _exec(platformDispatch(platform, cmd), options); |
| 61 | })(); |
| 62 | try { |
| 63 | return await Promise.race([currentExec, monitor.unexpectedExit]); |
| 64 | } finally { |
| 65 | monitor.disposeStdioListeners(); |
| 66 | if (lastExec === currentExec) { |
| 67 | lastExec = undefined; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | async function _exec(cmd: string, options?: { logLevel?: LogLevel; logOutput?: boolean | 'continuous' | 'silent'; stdin?: Buffer }) { |
| 73 | const text = `Run in ${hostName.toLowerCase()}: ${cmd.replace(/\n.*/g, '')}`; |
| 74 | let start: number; |
| 75 | if (options?.logOutput !== 'silent') { |
| 76 | start = output.start(text, options?.logLevel); |
| 77 | } |
| 78 | if (p.stdin.destroyed) { |