| 111 | } |
| 112 | |
| 113 | function runInteractiveCommand(command: string, args: string[], opts: RunWslOptions = {}, defaultTimeoutMs: number) { |
| 114 | return new Promise<WslCommandResult>((resolve, reject) => { |
| 115 | const child = pty.spawn(command, args, { |
| 116 | name: "xterm-color", |
| 117 | cols: 80, |
| 118 | rows: 24, |
| 119 | cwd: process.cwd(), |
| 120 | env: process.env, |
| 121 | useConpty: true, |
| 122 | }) |
| 123 | |
| 124 | let settled = false |
| 125 | let stdout = "" |
| 126 | |
| 127 | const cleanup = () => { |
| 128 | clearTimeout(timeoutId) |
| 129 | abortCleanup?.() |
| 130 | } |
| 131 | |
| 132 | const timeoutMs = opts.timeoutMs ?? defaultTimeoutMs |
| 133 | const timeoutId = setTimeout(() => { |
| 134 | try { |
| 135 | child.kill() |
| 136 | } catch { |
| 137 | /* ignore */ |
| 138 | } |
| 139 | if (settled) return |
| 140 | settled = true |
| 141 | cleanup() |
| 142 | reject(new Error(`${command} ${args.join(" ")} timed out after ${timeoutMs}ms`)) |
| 143 | }, timeoutMs) |
| 144 | |
| 145 | const abortHandler = () => { |
| 146 | try { |
| 147 | child.kill() |
| 148 | } catch { |
| 149 | /* ignore */ |
| 150 | } |
| 151 | if (settled) return |
| 152 | settled = true |
| 153 | cleanup() |
| 154 | reject(new DOMException("Aborted", "AbortError")) |
| 155 | } |
| 156 | const abortCleanup = opts.signal |
| 157 | ? (() => { |
| 158 | opts.signal?.addEventListener("abort", abortHandler, { once: true }) |
| 159 | return () => opts.signal?.removeEventListener("abort", abortHandler) |
| 160 | })() |
| 161 | : undefined |
| 162 | |
| 163 | child.onData((data: string) => { |
| 164 | stdout += data |
| 165 | }) |
| 166 | child.onExit((event: { exitCode: number }) => { |
| 167 | if (settled) return |
| 168 | settled = true |
| 169 | cleanup() |
| 170 | resolve({ code: event.exitCode, signal: null, stdout, stderr: "" }) |