|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
15 | 15 | import type * as nodePty from "node-pty";
|
| 16 | +import * as child_process from "child_process"; |
16 | 17 | import * as vscode from "vscode";
|
17 | 18 |
|
18 | 19 | import { requireNativeModule } from "../utilities/native";
|
@@ -153,3 +154,91 @@ export class SwiftPtyProcess implements SwiftProcess {
|
153 | 154 |
|
154 | 155 | onDidClose: vscode.Event<number | void> = this.closeEmitter.event;
|
155 | 156 | }
|
| 157 | + |
| 158 | +/** |
| 159 | + * A {@link SwiftProcess} that spawns a child process and does not bind to stdio. |
| 160 | + * |
| 161 | + * Use this for Swift tasks that do not need to accept input, as its lighter weight and |
| 162 | + * less error prone than using a spawned node-pty process. |
| 163 | + * |
| 164 | + * Specifically node-pty on Linux suffers from a long standing issue where the last chunk |
| 165 | + * of output before a program exits is sometimes dropped, especially if that program produces |
| 166 | + * a lot of output immediately before exiting. See https://github.com/microsoft/node-pty/issues/72 |
| 167 | + */ |
| 168 | +export class ReadOnlySwiftProcess implements SwiftProcess { |
| 169 | + private readonly spawnEmitter: vscode.EventEmitter<void> = new vscode.EventEmitter<void>(); |
| 170 | + private readonly writeEmitter: vscode.EventEmitter<string> = new vscode.EventEmitter<string>(); |
| 171 | + private readonly errorEmitter: vscode.EventEmitter<Error> = new vscode.EventEmitter<Error>(); |
| 172 | + private readonly closeEmitter: vscode.EventEmitter<number | void> = new vscode.EventEmitter< |
| 173 | + number | void |
| 174 | + >(); |
| 175 | + |
| 176 | + private spawnedProcess: child_process.ChildProcessWithoutNullStreams | undefined; |
| 177 | + |
| 178 | + constructor( |
| 179 | + public readonly command: string, |
| 180 | + public readonly args: string[], |
| 181 | + private readonly options: vscode.ProcessExecutionOptions = {} |
| 182 | + ) {} |
| 183 | + |
| 184 | + spawn(): void { |
| 185 | + try { |
| 186 | + this.spawnedProcess = child_process.spawn(this.command, this.args, { |
| 187 | + cwd: this.options.cwd, |
| 188 | + env: { ...process.env, ...this.options.env }, |
| 189 | + }); |
| 190 | + |
| 191 | + this.spawnedProcess.stdout.on("data", data => { |
| 192 | + this.writeEmitter.fire(data.toString()); |
| 193 | + }); |
| 194 | + |
| 195 | + this.spawnedProcess.stderr.on("data", data => { |
| 196 | + this.writeEmitter.fire(data.toString()); |
| 197 | + }); |
| 198 | + |
| 199 | + this.spawnedProcess.on("error", error => { |
| 200 | + this.errorEmitter.fire(new Error(`${error}`)); |
| 201 | + this.closeEmitter.fire(); |
| 202 | + }); |
| 203 | + |
| 204 | + this.spawnedProcess.once("exit", code => { |
| 205 | + this.closeEmitter.fire(code ?? undefined); |
| 206 | + this.dispose(); |
| 207 | + }); |
| 208 | + } catch (error) { |
| 209 | + this.errorEmitter.fire(new Error(`${error}`)); |
| 210 | + this.closeEmitter.fire(); |
| 211 | + this.dispose(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + handleInput(_s: string): void { |
| 216 | + // Do nothing |
| 217 | + } |
| 218 | + |
| 219 | + terminate(signal?: NodeJS.Signals): void { |
| 220 | + if (!this.spawnedProcess) { |
| 221 | + return; |
| 222 | + } |
| 223 | + this.spawnedProcess.kill(signal); |
| 224 | + this.dispose(); |
| 225 | + } |
| 226 | + |
| 227 | + setDimensions(_dimensions: vscode.TerminalDimensions): void { |
| 228 | + // Do nothing |
| 229 | + } |
| 230 | + |
| 231 | + dispose(): void { |
| 232 | + this.spawnedProcess?.stdout.removeAllListeners(); |
| 233 | + this.spawnedProcess?.stderr.removeAllListeners(); |
| 234 | + this.spawnedProcess?.removeAllListeners(); |
| 235 | + } |
| 236 | + |
| 237 | + onDidSpawn: vscode.Event<void> = this.spawnEmitter.event; |
| 238 | + |
| 239 | + onDidWrite: vscode.Event<string> = this.writeEmitter.event; |
| 240 | + |
| 241 | + onDidThrowError: vscode.Event<Error> = this.errorEmitter.event; |
| 242 | + |
| 243 | + onDidClose: vscode.Event<number | void> = this.closeEmitter.event; |
| 244 | +} |
0 commit comments