Skip to content

Commit 71d8bb3

Browse files
committed
better way to dealt with terminals in docker-compose
1 parent e999952 commit 71d8bb3

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/extension.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export let workspaceState: vscode.Memento;
6969
export let extensionContext: vscode.ExtensionContext;
7070
export let panel: vscode.StatusBarItem;
7171
export let posPanel: vscode.StatusBarItem;
72-
export let terminal: vscode.Terminal;
72+
export const terminals: vscode.Terminal[] = [];
7373
export let xmlContentProvider: XmlContentProvider;
7474

7575
import TelemetryReporter from "vscode-extension-telemetry";
@@ -250,6 +250,12 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
250250
checkConnection(true);
251251
}
252252
});
253+
vscode.window.onDidCloseTerminal((t) => {
254+
const terminalIndex = terminals.findIndex((terminal) => terminal.name == t.name);
255+
if (terminalIndex > -1) {
256+
terminals.splice(terminalIndex, 1);
257+
}
258+
});
253259

254260
workspace.onDidSaveTextDocument((file) => {
255261
if (schemas.includes(file.uri.scheme) || languages.includes(file.languageId)) {
@@ -492,7 +498,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
492498
export function deactivate(): void {
493499
// This will ensure all pending events get flushed
494500
reporter.dispose();
495-
if (terminal) {
496-
terminal.dispose();
501+
if (terminals) {
502+
terminals.forEach((t) => t.dispose());
497503
}
498504
}

src/utils/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path = require("path");
33
import * as url from "url";
44
import { execSync } from "child_process";
55
import * as vscode from "vscode";
6-
import { config, schemas, workspaceState } from "../extension";
6+
import { config, schemas, workspaceState, terminals } from "../extension";
77

88
export const outputChannel = vscode.window.createOutputChannel("ObjectScript");
99

@@ -137,7 +137,7 @@ export function notNull(el: any): boolean {
137137
}
138138

139139
export function portFromDockerCompose(): { port: number; docker: boolean } {
140-
const { "docker-compose": dockerCompose, port: defaultPort } = config("conn");
140+
const { "docker-compose": dockerCompose = {}, port: defaultPort } = config("conn");
141141
const result = { port: defaultPort, docker: false };
142142
const { service, file = "docker-compose.yml", internalPort = 52773 } = dockerCompose;
143143
if (!internalPort || !file || !service || service === "") {
@@ -183,16 +183,20 @@ export async function terminalWithDocker(): Promise<vscode.Terminal> {
183183
const workspace = currentWorkspaceFolder();
184184

185185
const terminalName = `ObjectScript:${workspace}`;
186-
const terminal = vscode.window.createTerminal(terminalName, "docker-compose", [
187-
"-f",
188-
file,
189-
"exec",
190-
service,
191-
"/bin/bash",
192-
"-c",
193-
`[ -f /tmp/vscodesession.pid ] && kill $(cat /tmp/vscodesession.pid) >/dev/null 2>&1 ; echo $$ > /tmp/vscodesession.pid;
194-
$(command -v ccontrol || command -v iris) session $ISC_PACKAGE_INSTANCENAME -U ${ns}`,
195-
]);
186+
let terminal = terminals.find((t) => t.name == terminalName && t.exitStatus == undefined);
187+
if (!terminal) {
188+
terminal = vscode.window.createTerminal(terminalName, "docker-compose", [
189+
"-f",
190+
file,
191+
"exec",
192+
service,
193+
"/bin/bash",
194+
"-c",
195+
`[ -f /tmp/vscodesession.pid ] && kill $(cat /tmp/vscodesession.pid) >/dev/null 2>&1 ; echo $$ > /tmp/vscodesession.pid;
196+
$(command -v ccontrol || command -v iris) session $ISC_PACKAGE_INSTANCENAME -U ${ns}`,
197+
]);
198+
terminals.push(terminal);
199+
}
196200
terminal.show(true);
197201
return terminal;
198202
}

0 commit comments

Comments
 (0)