Skip to content

Commit

Permalink
yet another terminal tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
rewrking committed Nov 18, 2023
1 parent 17b692f commit 0e170e6
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions Extension/src/Terminal/TerminalProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class TerminalProcess {
private subprocess: Optional<proc.ChildProcessByStdio<Writable, Readable, Readable>> = null;
private interrupted: boolean = false;
private killed: boolean = false;
private completed: boolean = false;

private shellPath: string = "";
private label: string = "";
Expand Down Expand Up @@ -69,16 +68,6 @@ class TerminalProcess {
}
};

private clearListeners = (): void => {
if (this.subprocess) {
// Note: Fixes a bug where if the process is recreated, the old listeners don't get fired
this.subprocess.stdout.removeAllListeners("data");
this.subprocess.stderr.removeAllListeners("data");
this.subprocess.removeAllListeners("close");
this.subprocess.removeAllListeners("error");
}
};

private haltSubProcess = (onHalt?: () => void): void => {
if (!!this.subprocess?.pid && !this.killed) {
const signal: NodeJS.Signals = "SIGINT";
Expand All @@ -88,14 +77,22 @@ class TerminalProcess {
// Note: We don't care about the error
// if (!!err) OutputChannel.logError(err);

if (!!err) {
console.error(err);
console.error("there was an error halting the process");
if (this.platform === VSCodePlatform.Windows) {
if (!!err) {
console.error(err);
console.error("there was an error halting the process");
} else {
this.subprocess = null;
onHalt?.();
}
} else {
if (!!err) {
console.error(err);
console.error("there was an error halting the process");
}
this.subprocess = null;
onHalt?.();
}

this.clearListeners();
this.subprocess = null;
onHalt?.();
};

if (this.platform === VSCodePlatform.Windows) {
Expand Down Expand Up @@ -235,11 +232,16 @@ class TerminalProcess {
}: TerminalProcessOptions): Promise<number> => {
return new Promise((resolve, reject) => {
const onCreate = () => {
this.clearListeners();
if (!!this.subprocess) {
// Note: Fixes a bug where if the process is recreated, the old listeners don't get fired
this.subprocess.stdout.removeAllListeners("data");
this.subprocess.stderr.removeAllListeners("data");
this.subprocess.removeAllListeners("close");
this.subprocess.removeAllListeners("error");
}

this.killed = false;
this.interrupted = false;
this.completed = false;

// console.log(cwd);
// console.log(env);
Expand Down Expand Up @@ -286,7 +288,6 @@ class TerminalProcess {
this.subprocess.on("close", (code, signal) => {
try {
this.onProcessClose(code, signal);
this.completed = true;
onSuccess?.(code ?? 0, signal);
resolve(code ?? 0);
} catch (err) {
Expand All @@ -295,11 +296,6 @@ class TerminalProcess {
});
};

if (!!this.subprocess && !this.completed) {
this.interrupted = true;
this.onProcessClose(null, "SIGINT");
}

this.haltSubProcess(onCreate);
});
};
Expand Down

0 comments on commit 0e170e6

Please sign in to comment.