@noldova/terminal 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/menus.json +53 -0
- package/module.json +28 -0
- package/package.json +50 -4
- package/settings.json +83 -0
- package/src/api/index.d.ts +131 -0
- package/src/api/index.js +11 -0
- package/src/api/window.d.ts +467 -0
- package/src/api/window.js +8 -0
- package/src/enums/escape-state.js +15 -0
- package/src/enums/escape-stripper-state.js +15 -0
- package/src/enums/mark-kind.js +18 -0
- package/src/enums/shell-family.js +14 -0
- package/src/enums/system-requirement.js +11 -0
- package/src/exceptions/agent-terminal-restart.exception.js +16 -0
- package/src/exceptions/command-exited.exception.js +23 -0
- package/src/exceptions/command-failed.exception.js +13 -0
- package/src/exceptions/command-not-started.exception.js +17 -0
- package/src/exceptions/command-output-too-long.exception.js +18 -0
- package/src/exceptions/command-timed-out.exception.js +18 -0
- package/src/exceptions/command-variable-not-set.exception.js +18 -0
- package/src/exceptions/default-shell-not-found.exception.js +14 -0
- package/src/exceptions/environment-export-invalid.exception.js +14 -0
- package/src/exceptions/environment-key-unreadable.exception.js +17 -0
- package/src/exceptions/environment-unreadable.exception.js +13 -0
- package/src/exceptions/folder-not-found.exception.js +16 -0
- package/src/exceptions/host-ended.exception.js +14 -0
- package/src/exceptions/session-not-found.exception.js +18 -0
- package/src/exceptions/shell-kind-not-found.exception.js +16 -0
- package/src/exceptions/shell-not-found.exception.js +18 -0
- package/src/exceptions/shells-unreadable.exception.js +13 -0
- package/src/exceptions/stored-lines-damaged.exception.js +15 -0
- package/src/exceptions/stored-lines-unwritable.exception.js +15 -0
- package/src/exceptions/system-root-missing.exception.js +14 -0
- package/src/exceptions/terminal.exception.js +13 -0
- package/src/exceptions/unknown-method.exception.js +16 -0
- package/src/exceptions/unsupported-system.exception.js +18 -0
- package/src/exceptions/visual-studio-unreadable.exception.js +15 -0
- package/src/exceptions/wsl-distributions-unreadable.exception.js +15 -0
- package/src/interfaces/i-command-runner.js +8 -0
- package/src/interfaces/i-disposable.js +8 -0
- package/src/interfaces/i-environment-source.js +8 -0
- package/src/interfaces/i-file-system-reader.js +8 -0
- package/src/interfaces/i-file-system-writer.js +8 -0
- package/src/interfaces/i-flow-controller-listener.js +8 -0
- package/src/interfaces/i-folder-access.js +8 -0
- package/src/interfaces/i-line-process-listener.js +8 -0
- package/src/interfaces/i-line-process-starter.js +8 -0
- package/src/interfaces/i-line-process.js +8 -0
- package/src/interfaces/i-output-batcher-listener.js +8 -0
- package/src/interfaces/i-process-signaller.js +8 -0
- package/src/interfaces/i-pseudo-terminal-listener.js +8 -0
- package/src/interfaces/i-pty-exit-event.js +8 -0
- package/src/interfaces/i-pty-process.js +8 -0
- package/src/interfaces/i-pty-spawner.js +8 -0
- package/src/interfaces/i-terminal-mirror-listener.js +8 -0
- package/src/interfaces/i-terminal-session-listener.js +8 -0
- package/src/interfaces/i-waiting-options-request.js +8 -0
- package/src/models/command-listing.js +17 -0
- package/src/models/completion-candidate.js +19 -0
- package/src/models/environment-tools.js +23 -0
- package/src/models/environment-variable.js +15 -0
- package/src/models/folder-entry.js +15 -0
- package/src/models/hosted-terminal.js +116 -0
- package/src/models/mirror-line.js +27 -0
- package/src/models/mirror-snapshot.js +19 -0
- package/src/models/one-shot-launch.js +42 -0
- package/src/models/pane-progress.js +15 -0
- package/src/models/powershell-cmdlet.js +21 -0
- package/src/models/powershell-lookup.js +17 -0
- package/src/models/powershell-parameter.js +19 -0
- package/src/models/registry-value.js +15 -0
- package/src/models/request-client.js +23 -0
- package/src/models/saved-terminal.js +25 -0
- package/src/models/shell-launch.js +23 -0
- package/src/models/shell-line.js +21 -0
- package/src/models/shell-mark.js +19 -0
- package/src/models/shell-options-reply.js +17 -0
- package/src/models/shell.js +29 -0
- package/src/models/started-shell.js +21 -0
- package/src/models/system-shortfall.js +15 -0
- package/src/models/terminal-events.js +37 -0
- package/src/models/terminal-history-paths.js +15 -0
- package/src/models/terminal-prompt.js +28 -0
- package/src/models/tracked-command.js +26 -0
- package/src/models/typed-line.js +40 -0
- package/src/models/visual-studio-installation.js +22 -0
- package/src/models/waiting-run.js +25 -0
- package/src/resources.js +1725 -0
- package/src/runtime-part.js +84 -0
- package/src/services/agent-terminal.js +137 -0
- package/src/services/agent-terminals.js +55 -0
- package/src/services/bounded-reads.js +57 -0
- package/src/services/change-signal.js +33 -0
- package/src/services/command-history.js +20 -0
- package/src/services/command-runner.js +67 -0
- package/src/services/command-tracker.js +161 -0
- package/src/services/completion-source.js +316 -0
- package/src/services/context-pty-process.js +79 -0
- package/src/services/context-pty-spawner.js +26 -0
- package/src/services/deferred-pty-process.js +94 -0
- package/src/services/escape-sequence-tracker.js +126 -0
- package/src/services/escape-stripper.js +91 -0
- package/src/services/failure-text.js +15 -0
- package/src/services/file-system-writer.js +35 -0
- package/src/services/file-system.reader.js +54 -0
- package/src/services/flow-controller.js +102 -0
- package/src/services/folder-access.js +19 -0
- package/src/services/inherited-environment.js +20 -0
- package/src/services/line-process-starter.js +17 -0
- package/src/services/line-process.js +72 -0
- package/src/services/log-file.js +57 -0
- package/src/services/mac-environment.js +81 -0
- package/src/services/mac-folder-access.js +17 -0
- package/src/services/mark-scanner.js +94 -0
- package/src/services/one-shot-command.js +119 -0
- package/src/services/output-batcher.js +62 -0
- package/src/services/output-log.js +50 -0
- package/src/services/powershell-binding.js +61 -0
- package/src/services/process-signaller.js +19 -0
- package/src/services/pseudo-terminal.js +54 -0
- package/src/services/registry-export.js +108 -0
- package/src/services/resize-wakeup.js +39 -0
- package/src/services/saved-terminals.js +52 -0
- package/src/services/shared-reads.js +41 -0
- package/src/services/shell-command.js +90 -0
- package/src/services/shell-environment.js +20 -0
- package/src/services/shell-integration.js +207 -0
- package/src/services/shell-locator.js +177 -0
- package/src/services/shell-options-queue.js +34 -0
- package/src/services/shell-words.js +80 -0
- package/src/services/system-requirements.js +46 -0
- package/src/services/terminal-audience.js +83 -0
- package/src/services/terminal-history-folder.js +97 -0
- package/src/services/terminal-history-segment.js +109 -0
- package/src/services/terminal-history.js +130 -0
- package/src/services/terminal-host.js +931 -0
- package/src/services/terminal-line-reader.js +101 -0
- package/src/services/terminal-mirror.js +221 -0
- package/src/services/terminal-owner.service.js +27 -0
- package/src/services/terminal-session.js +398 -0
- package/src/services/visual-studio.reader.js +76 -0
- package/src/services/windows-environment.js +218 -0
- package/src/services/windows-folder-access.js +23 -0
- package/src/services/wsl-distribution.reader.js +49 -0
- package/src/services/xterm-headless-loader.js +14 -0
- package/src/tools/enums/command-status.js +13 -0
- package/src/tools/enums/run-method.js +11 -0
- package/src/tools/exceptions/abort-reason-unknown.exception.js +17 -0
- package/src/tools/exceptions/stale-terminal.exception.js +16 -0
- package/src/tools/interfaces/i-agent-command.js +8 -0
- package/src/tools/interfaces/i-agent-terminal.js +8 -0
- package/src/tools/interfaces/i-agent-terminals.js +8 -0
- package/src/tools/interfaces/i-output-source.js +8 -0
- package/src/tools/interfaces/i-terminal-prompt.js +8 -0
- package/src/tools/models/agent-terminal-access.js +25 -0
- package/src/tools/models/agent-terminal-opening.js +29 -0
- package/src/tools/models/command-line.js +28 -0
- package/src/tools/models/key-sequence.js +27 -0
- package/src/tools/models/output-chunk.js +18 -0
- package/src/tools/models/output-excerpt.js +31 -0
- package/src/tools/models/permission-ask.js +34 -0
- package/src/tools/models/read-cursor.js +26 -0
- package/src/tools/models/read-cursors.js +25 -0
- package/src/tools/models/terminal-open-input.js +34 -0
- package/src/tools/models/terminal-output-report.js +61 -0
- package/src/tools/models/terminal-read-input.js +39 -0
- package/src/tools/models/terminal-run-input.js +52 -0
- package/src/tools/models/terminal-target-input.js +34 -0
- package/src/tools/models/terminal-write-input.js +38 -0
- package/src/tools/models/tool-call-context.js +61 -0
- package/src/tools/services/abort-reason-reader.js +25 -0
- package/src/tools/services/permission-details.js +42 -0
- package/src/tools/services/permission-gate.js +35 -0
- package/src/tools/services/signal-race.js +24 -0
- package/src/tools/services/terminal-access-policy.js +57 -0
- package/src/tools/services/terminal-close-tool.js +57 -0
- package/src/tools/services/terminal-interrupt-tool.js +57 -0
- package/src/tools/services/terminal-list-tool.js +69 -0
- package/src/tools/services/terminal-open-tool.js +71 -0
- package/src/tools/services/terminal-read-tool.js +58 -0
- package/src/tools/services/terminal-run-tool.js +78 -0
- package/src/tools/services/terminal-text-escaper.js +40 -0
- package/src/tools/services/terminal-tool.js +47 -0
- package/src/tools/services/terminal-tools.js +60 -0
- package/src/tools/services/terminal-write-tool.js +75 -0
- package/src/tools/services/tool-failures.js +28 -0
- package/src/tools/services/tool-results.js +13 -0
|
@@ -0,0 +1,931 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
import { ArgumentException } from "@noldova/context.exceptions";
|
|
10
|
+
import { EndedBy, Resources as ProtocolResources, TerminalAckParams, TerminalCommandAdded, TerminalCommandEnded, TerminalCompleteParams, TerminalCompletion, TerminalExit, TerminalExited, TerminalFindCommandsParams, TerminalInputParams, TerminalLaunched, TerminalLaunchParams, TerminalLinesParams, TerminalName, TerminalNames, TerminalOpenParams, TerminalOwnerReference, TerminalPaneReference, TerminalReference, TerminalResizeParams, TerminalRestartParams, TerminalRestoreParams, TerminalRunBusy, TerminalRunEnded, TerminalRunEndedBy, TerminalRunOutcome, TerminalRunOutputParams, TerminalRunParams, TerminalRunResult, TerminalSessions, TerminalShellKind, TerminalShells, TerminalShowOwnerParams, TerminalSize, TerminalVisibilityParams, WindowsPty } from "@noldova/terminal.protocol";
|
|
11
|
+
import { AgentTerminalRestartException } from "../exceptions/agent-terminal-restart.exception.js";
|
|
12
|
+
import { SystemRequirement } from "../enums/system-requirement.js";
|
|
13
|
+
import { DefaultShellNotFoundException } from "../exceptions/default-shell-not-found.exception.js";
|
|
14
|
+
import { FolderNotFoundException } from "../exceptions/folder-not-found.exception.js";
|
|
15
|
+
import { HostEndedException } from "../exceptions/host-ended.exception.js";
|
|
16
|
+
import { SessionNotFoundException } from "../exceptions/session-not-found.exception.js";
|
|
17
|
+
import { ShellKindNotFoundException } from "../exceptions/shell-kind-not-found.exception.js";
|
|
18
|
+
import { ShellNotFoundException } from "../exceptions/shell-not-found.exception.js";
|
|
19
|
+
import { UnknownMethodException } from "../exceptions/unknown-method.exception.js";
|
|
20
|
+
import { UnsupportedSystemException } from "../exceptions/unsupported-system.exception.js";
|
|
21
|
+
import { HostedTerminal } from "../models/hosted-terminal.js";
|
|
22
|
+
import { SavedTerminal } from "../models/saved-terminal.js";
|
|
23
|
+
import { ShellOptionsReply } from "../models/shell-options-reply.js";
|
|
24
|
+
import { StartedShell } from "../models/started-shell.js";
|
|
25
|
+
import { WaitingRun } from "../models/waiting-run.js";
|
|
26
|
+
import { Resources } from "../resources.js";
|
|
27
|
+
import { CommandHistory } from "./command-history.js";
|
|
28
|
+
import { DeferredPtyProcess } from "./deferred-pty-process.js";
|
|
29
|
+
import { FailureText } from "./failure-text.js";
|
|
30
|
+
import { SystemRequirements } from "./system-requirements.js";
|
|
31
|
+
import { TerminalAudience } from "./terminal-audience.js";
|
|
32
|
+
import { TerminalSession } from "./terminal-session.js";
|
|
33
|
+
export class TerminalHost {
|
|
34
|
+
spawner;
|
|
35
|
+
fileSystem;
|
|
36
|
+
integration;
|
|
37
|
+
historyFolder;
|
|
38
|
+
locator;
|
|
39
|
+
windowsPty;
|
|
40
|
+
environmentSource;
|
|
41
|
+
homeFolder;
|
|
42
|
+
log;
|
|
43
|
+
events;
|
|
44
|
+
saved;
|
|
45
|
+
completions;
|
|
46
|
+
sessions = new Map();
|
|
47
|
+
commandHistory = new CommandHistory();
|
|
48
|
+
disposals = new Set();
|
|
49
|
+
opens = new Set();
|
|
50
|
+
launches = new Map();
|
|
51
|
+
agentSessionOwners = new Map();
|
|
52
|
+
audience = new TerminalAudience();
|
|
53
|
+
panelOpenings = new Map();
|
|
54
|
+
hasEnded = false;
|
|
55
|
+
restoring = null;
|
|
56
|
+
appCloses = new Set();
|
|
57
|
+
constructor(spawner, fileSystem, integration, historyFolder, locator, platform, osRelease, environmentSource, homeFolder, log, events, saved, completions) {
|
|
58
|
+
this.spawner = spawner;
|
|
59
|
+
this.fileSystem = fileSystem;
|
|
60
|
+
this.integration = integration;
|
|
61
|
+
integration.onSuggestingChanged(t => {
|
|
62
|
+
if (!t)
|
|
63
|
+
this.commandHistory.clear();
|
|
64
|
+
});
|
|
65
|
+
this.historyFolder = historyFolder;
|
|
66
|
+
this.locator = locator;
|
|
67
|
+
this.windowsPty = platform === Resources.windowsPlatform
|
|
68
|
+
? new WindowsPty(TerminalHost.readWindowsBuild(osRelease))
|
|
69
|
+
: null;
|
|
70
|
+
this.environmentSource = environmentSource;
|
|
71
|
+
this.homeFolder = homeFolder;
|
|
72
|
+
this.log = log;
|
|
73
|
+
this.events = events;
|
|
74
|
+
this.saved = saved;
|
|
75
|
+
this.completions = completions;
|
|
76
|
+
}
|
|
77
|
+
async handleAsync(method, payload, client) {
|
|
78
|
+
this.audience.see(client);
|
|
79
|
+
switch (method) {
|
|
80
|
+
case TerminalNames.shellsMethod:
|
|
81
|
+
return new TerminalShells((await this.locator.locateAsync()).map(t => t.toTerminalShell())).toJson();
|
|
82
|
+
case TerminalNames.openMethod:
|
|
83
|
+
return (await this.openAsync(TerminalOpenParams.fromJson(payload), client)).toJson();
|
|
84
|
+
case TerminalNames.closeMethod:
|
|
85
|
+
await this.closeAsync(TerminalReference.fromJson(payload).sessionId, client);
|
|
86
|
+
return null;
|
|
87
|
+
case TerminalNames.restartMethod:
|
|
88
|
+
await this.restartAsync(TerminalRestartParams.fromJson(payload), client);
|
|
89
|
+
return null;
|
|
90
|
+
case TerminalNames.sessionsMethod:
|
|
91
|
+
return new TerminalSessions(this.terminals.filter(t => t.isReachedBy(client.connection, client.isApp))
|
|
92
|
+
.map(t => t.session.toSummary(t.folder, t.ownerSessionId !== null, t.isPanelTerminal, t.owner, t.opensTab, t.name, t.shell.name))).toJson();
|
|
93
|
+
case TerminalNames.attachMethod:
|
|
94
|
+
return (await this.attachAsync(TerminalPaneReference.fromJson(payload), client)).toJson();
|
|
95
|
+
case TerminalNames.inputMethod:
|
|
96
|
+
this.input(TerminalInputParams.fromJson(payload), client);
|
|
97
|
+
return null;
|
|
98
|
+
case TerminalNames.resizeMethod:
|
|
99
|
+
this.resize(TerminalResizeParams.fromJson(payload), client);
|
|
100
|
+
return null;
|
|
101
|
+
case TerminalNames.ackMethod:
|
|
102
|
+
this.acknowledge(TerminalAckParams.fromJson(payload), client);
|
|
103
|
+
return null;
|
|
104
|
+
case TerminalNames.visibilityMethod:
|
|
105
|
+
this.setVisible(TerminalVisibilityParams.fromJson(payload), client);
|
|
106
|
+
return null;
|
|
107
|
+
case TerminalNames.detachMethod:
|
|
108
|
+
this.detach(TerminalPaneReference.fromJson(payload), client);
|
|
109
|
+
return null;
|
|
110
|
+
case TerminalNames.linesMethod:
|
|
111
|
+
return (await this.readLinesAsync(TerminalLinesParams.fromJson(payload), client)).toJson();
|
|
112
|
+
case TerminalNames.restoreMethod:
|
|
113
|
+
await this.restoreAsync(TerminalRestoreParams.fromJson(payload), client);
|
|
114
|
+
return null;
|
|
115
|
+
case TerminalNames.closeOwnerMethod:
|
|
116
|
+
await this.closeOwnerAsync(TerminalOwnerReference.fromJson(payload).owner, client);
|
|
117
|
+
return null;
|
|
118
|
+
case TerminalNames.launchMethod:
|
|
119
|
+
return (await this.trackLaunchAsync(this.trackAsync(this.launchAsync(TerminalLaunchParams.fromJson(payload), client)), client)).toJson();
|
|
120
|
+
case TerminalNames.runMethod:
|
|
121
|
+
return (await this.trackAsync(this.runAsync(TerminalRunParams.fromJson(payload), client))).toJson();
|
|
122
|
+
case TerminalNames.runOutputMethod:
|
|
123
|
+
return (await this.readRunOutputAsync(TerminalRunOutputParams.fromJson(payload), client))?.toJson() ?? null;
|
|
124
|
+
case TerminalNames.renameMethod:
|
|
125
|
+
this.rename(TerminalName.fromJson(payload), client);
|
|
126
|
+
return null;
|
|
127
|
+
case TerminalNames.completeMethod:
|
|
128
|
+
return (await this.completeAsync(TerminalCompleteParams.fromJson(payload), client)).toJson();
|
|
129
|
+
case TerminalNames.findCommandsMethod:
|
|
130
|
+
return (await this.findCommandsAsync(TerminalFindCommandsParams.fromJson(payload), client)).toJson();
|
|
131
|
+
case TerminalNames.windowStartedMethod:
|
|
132
|
+
if (client.window !== null)
|
|
133
|
+
await this.endLaunchesWithoutTabAsync(client.connection, client.window, client.page);
|
|
134
|
+
return null;
|
|
135
|
+
case TerminalNames.showOwnerMethod: {
|
|
136
|
+
const shown = TerminalShowOwnerParams.fromJson(payload);
|
|
137
|
+
this.audience.show(client, shown.owner, shown.isHidden);
|
|
138
|
+
this.startUnshownShells();
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
default:
|
|
142
|
+
throw new UnknownMethodException(method);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async endAllAsync() {
|
|
146
|
+
this.hasEnded = true;
|
|
147
|
+
for (const terminal of this.terminals) {
|
|
148
|
+
terminal.cancelCommand();
|
|
149
|
+
this.endRun(terminal, TerminalRunEndedBy.TerminalEnded, null);
|
|
150
|
+
}
|
|
151
|
+
const sessions = this.terminals.map(t => t.session);
|
|
152
|
+
this.sessions.clear();
|
|
153
|
+
await Promise.all(sessions.map(t => this.endAsync(t)));
|
|
154
|
+
await Promise.all(this.disposals);
|
|
155
|
+
await this.historyFolder.removeAsync();
|
|
156
|
+
try {
|
|
157
|
+
await this.integration.disposeAsync();
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
this.log.write(Resources.formatIntegrationRemovalFailed(FailureText.read(error)));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
get terminals() {
|
|
164
|
+
return [...this.sessions.values()];
|
|
165
|
+
}
|
|
166
|
+
findTerminal(sessionId) {
|
|
167
|
+
return this.sessions.get(sessionId);
|
|
168
|
+
}
|
|
169
|
+
openTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit = ProtocolResources.defaultStoredLimit, note = null, isPanelTerminal = false, owner = null, client = null, sessionId = randomUUID(), environment = null, startsAtFit = false, opensTab = true) {
|
|
170
|
+
return this.trackAsync(this.startTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit, note, isPanelTerminal, owner, client, sessionId, environment, startsAtFit, opensTab));
|
|
171
|
+
}
|
|
172
|
+
bindAgentSession(agentSessionId, owner) {
|
|
173
|
+
this.agentSessionOwners.set(agentSessionId, owner);
|
|
174
|
+
}
|
|
175
|
+
unbindAgentSession(agentSessionId) {
|
|
176
|
+
this.agentSessionOwners.delete(agentSessionId);
|
|
177
|
+
}
|
|
178
|
+
findAgentSessionOwner(agentSessionId) {
|
|
179
|
+
return this.agentSessionOwners.get(agentSessionId) ?? null;
|
|
180
|
+
}
|
|
181
|
+
async spawnAsync(file, args, folder, size, environment = {}) {
|
|
182
|
+
return this.spawnFromAsync(await this.readEnvironmentAsync(), file, args, folder, size, environment, false);
|
|
183
|
+
}
|
|
184
|
+
async closeOwnerAsync(owner, client) {
|
|
185
|
+
const isApp = client === null || client.isApp;
|
|
186
|
+
if (isApp)
|
|
187
|
+
for (const [agentSessionId, bound] of this.agentSessionOwners)
|
|
188
|
+
if (bound === owner)
|
|
189
|
+
this.agentSessionOwners.delete(agentSessionId);
|
|
190
|
+
const isOwned = (terminal) => terminal.owner === owner
|
|
191
|
+
&& (client === null || terminal.isReachedBy(client.connection, client.isApp));
|
|
192
|
+
for (const terminal of this.terminals.filter(isOwned))
|
|
193
|
+
terminal.cancelCommand();
|
|
194
|
+
await Promise.allSettled([this.restoring]);
|
|
195
|
+
await Promise.allSettled(this.opens);
|
|
196
|
+
this.ensureRunning();
|
|
197
|
+
const owned = this.terminals.filter(isOwned);
|
|
198
|
+
if (isApp)
|
|
199
|
+
this.saved.forgetOwner(owner);
|
|
200
|
+
for (const terminal of owned) {
|
|
201
|
+
this.sessions.delete(terminal.session.sessionId);
|
|
202
|
+
terminal.cancelCommand();
|
|
203
|
+
this.endRun(terminal, TerminalRunEndedBy.TerminalEnded, null);
|
|
204
|
+
this.audience.publish(this.events.closed, terminal, new TerminalReference(terminal.session.sessionId).toJson());
|
|
205
|
+
}
|
|
206
|
+
await Promise.all(owned.map(t => this.endAsync(t.session)));
|
|
207
|
+
}
|
|
208
|
+
async closeConnectionAsync(connection, window) {
|
|
209
|
+
const wasApp = this.audience.forget(connection, window);
|
|
210
|
+
this.startUnshownShells();
|
|
211
|
+
if (window !== null) {
|
|
212
|
+
await this.endLaunchesWithoutTabAsync(connection, window, null);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const closing = this.endConnectionAsync(connection, wasApp);
|
|
216
|
+
if (wasApp)
|
|
217
|
+
this.appCloses.add(closing);
|
|
218
|
+
try {
|
|
219
|
+
await closing;
|
|
220
|
+
}
|
|
221
|
+
finally {
|
|
222
|
+
this.appCloses.delete(closing);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async closeTerminalAsync(sessionId) {
|
|
226
|
+
const terminal = this.sessions.get(sessionId);
|
|
227
|
+
if (terminal === undefined)
|
|
228
|
+
return;
|
|
229
|
+
this.sessions.delete(sessionId);
|
|
230
|
+
terminal.cancelCommand();
|
|
231
|
+
this.endRun(terminal, TerminalRunEndedBy.TerminalEnded, null);
|
|
232
|
+
this.saved.forget(sessionId);
|
|
233
|
+
if (terminal.owner !== null)
|
|
234
|
+
this.audience.publish(this.events.closed, terminal, new TerminalReference(sessionId).toJson());
|
|
235
|
+
if ((!await this.stopAsync(terminal.session) || terminal.heldExit !== null) && terminal.environment !== null) {
|
|
236
|
+
terminal.isEndSent = true;
|
|
237
|
+
this.audience.publish(this.events.exited, terminal, new TerminalExited(sessionId, new TerminalExit(null, EndedBy.Close)).toJson());
|
|
238
|
+
}
|
|
239
|
+
await this.disposeAsync(terminal.session);
|
|
240
|
+
}
|
|
241
|
+
async startTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit, note, isPanelTerminal, owner, client, sessionId, environment, startsAtFit, opensTab) {
|
|
242
|
+
const shell = await this.locator.findAsync(shellId);
|
|
243
|
+
this.ensureRunning();
|
|
244
|
+
if (shell === undefined)
|
|
245
|
+
throw shellId === null ? new DefaultShellNotFoundException() : new ShellNotFoundException(shellId);
|
|
246
|
+
const isFolder = await this.fileSystem.isFolderAsync(folder);
|
|
247
|
+
this.ensureRunning();
|
|
248
|
+
if (!isFolder)
|
|
249
|
+
throw new FolderNotFoundException(folder);
|
|
250
|
+
const historyPaths = await this.historyFolder.pathsAsync(sessionId);
|
|
251
|
+
this.ensureRunning();
|
|
252
|
+
const started = await this.startShellAsync(shell, folder, size, note, environment ?? {}, startsAtFit ? sessionId : null);
|
|
253
|
+
const isKept = ownerSessionId === null && client?.isApp === true && environment === null;
|
|
254
|
+
const publish = (channel, payload) => this.audience.publish(channel, terminal, payload);
|
|
255
|
+
const terminal = new HostedTerminal(new TerminalSession(sessionId, shell.id, size, started.process, started.nonce, this.windowsPty, historyPaths, storedLimit, {
|
|
256
|
+
onOutput: t => publish(this.events.output, t.toJson()),
|
|
257
|
+
onExited: t => {
|
|
258
|
+
this.endRun(terminal, TerminalRunEndedBy.TerminalEnded, null);
|
|
259
|
+
if (terminal.isEndSent)
|
|
260
|
+
return;
|
|
261
|
+
if (terminal.isRestarting) {
|
|
262
|
+
terminal.heldExit = t;
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
publish(this.events.exited, t.toJson());
|
|
266
|
+
this.forgetOldestExited();
|
|
267
|
+
},
|
|
268
|
+
onTitleChanged: t => publish(this.events.titleChanged, t.toJson()),
|
|
269
|
+
onCwdChanged: t => {
|
|
270
|
+
if (isKept)
|
|
271
|
+
this.saved.moveTo(t.sessionId, t.cwd);
|
|
272
|
+
publish(this.events.cwdChanged, t.toJson());
|
|
273
|
+
},
|
|
274
|
+
onCommandEnded: t => {
|
|
275
|
+
this.endLaunchCommand(terminal, t.exitCode);
|
|
276
|
+
this.countRunEnd(terminal, t.exitCode, true);
|
|
277
|
+
},
|
|
278
|
+
onPromptStarted: () => {
|
|
279
|
+
const offset = terminal.launchCommandOffset;
|
|
280
|
+
if (offset !== null && terminal.session.text.readRange(offset, terminal.session.text.endOffset).includes(Resources.lineFeed))
|
|
281
|
+
this.endLaunchCommand(terminal, null);
|
|
282
|
+
this.countRunEnd(terminal, null, false);
|
|
283
|
+
},
|
|
284
|
+
onCommandStarted: t => this.addCommand(shell.id, t),
|
|
285
|
+
onDetached: t => publish(this.events.detached, t.toJson()),
|
|
286
|
+
onFailed: (id, message) => this.log.write(Resources.formatStartedCommandUnread(id, message))
|
|
287
|
+
}, Resources.maximumAgentTextLength, shell.kind, ownerSessionId === null), shell, folder, ownerSessionId, isPanelTerminal, owner, client?.connection ?? null, environment);
|
|
288
|
+
terminal.deferredShell = started.deferred;
|
|
289
|
+
terminal.startTemporaryFolder = started.temporaryFolder;
|
|
290
|
+
for (const line of started.printsNote ? [] : note?.split(Resources.lineBreaks) ?? [])
|
|
291
|
+
terminal.session.writeNote(line);
|
|
292
|
+
terminal.opensTab = opensTab;
|
|
293
|
+
this.sessions.set(sessionId, terminal);
|
|
294
|
+
if (isKept)
|
|
295
|
+
this.saved.save(new SavedTerminal(sessionId, shell.id, folder, isPanelTerminal, owner, opensTab));
|
|
296
|
+
else if (ownerSessionId !== null && owner !== null)
|
|
297
|
+
this.audience.publish(this.events.opened, terminal, terminal.session.toSummary(folder, true, isPanelTerminal, owner, true, null, shell.name).toJson());
|
|
298
|
+
return terminal;
|
|
299
|
+
}
|
|
300
|
+
async endLaunchesWithoutTabAsync(connection, window, page) {
|
|
301
|
+
await Promise.allSettled([...this.launches].filter(([, t]) => t.connection === connection && t.window === window).map(([t]) => t));
|
|
302
|
+
await Promise.all(this.terminals
|
|
303
|
+
.filter(t => t.connection === connection && t.launcher?.window === window && (page === null || t.launcher.page !== page))
|
|
304
|
+
.map(t => this.closeTerminalAsync(t.session.sessionId)));
|
|
305
|
+
}
|
|
306
|
+
trackLaunchAsync(launching, client) {
|
|
307
|
+
this.launches.set(launching, client);
|
|
308
|
+
const settled = () => void this.launches.delete(launching);
|
|
309
|
+
void launching.then(settled, settled);
|
|
310
|
+
return launching;
|
|
311
|
+
}
|
|
312
|
+
async endConnectionAsync(connection, wasApp) {
|
|
313
|
+
const isEnding = (terminal) => terminal.connection === null ? wasApp && terminal.owner !== null
|
|
314
|
+
: terminal.connection === connection;
|
|
315
|
+
for (const terminal of this.terminals.filter(isEnding))
|
|
316
|
+
terminal.cancelCommand();
|
|
317
|
+
await Promise.allSettled([this.restoring]);
|
|
318
|
+
await Promise.allSettled(this.opens);
|
|
319
|
+
if (this.hasEnded)
|
|
320
|
+
return;
|
|
321
|
+
const ending = this.terminals.filter(isEnding);
|
|
322
|
+
for (const terminal of ending) {
|
|
323
|
+
this.sessions.delete(terminal.session.sessionId);
|
|
324
|
+
terminal.cancelCommand();
|
|
325
|
+
}
|
|
326
|
+
if (wasApp)
|
|
327
|
+
this.restoring = null;
|
|
328
|
+
await Promise.all(ending.map(t => this.endAsync(t.session)));
|
|
329
|
+
}
|
|
330
|
+
trackAsync(opening) {
|
|
331
|
+
this.opens.add(opening);
|
|
332
|
+
const settled = () => void this.opens.delete(opening);
|
|
333
|
+
void opening.then(settled, settled);
|
|
334
|
+
return opening;
|
|
335
|
+
}
|
|
336
|
+
async launchAsync(params, client) {
|
|
337
|
+
if (params.folder !== null && !this.isAbsolute(params.folder))
|
|
338
|
+
throw new ArgumentException(ProtocolResources.formatNotAbsolutePath(ProtocolResources.valueSubject), Resources.folderParameter);
|
|
339
|
+
const folder = params.folder ?? this.homeFolder;
|
|
340
|
+
const isFolder = await this.fileSystem.isFolderAsync(folder);
|
|
341
|
+
this.ensureRunning();
|
|
342
|
+
if (!isFolder)
|
|
343
|
+
return new TerminalLaunched(null, folder);
|
|
344
|
+
let shellId = params.shellId;
|
|
345
|
+
if (params.shellKinds.length > 0) {
|
|
346
|
+
const shell = await this.findShellOfKindsAsync(params.shellKinds, params.shellId);
|
|
347
|
+
this.ensureRunning();
|
|
348
|
+
if (shell === undefined)
|
|
349
|
+
throw new ShellKindNotFoundException(params.shellKinds);
|
|
350
|
+
shellId = shell.id;
|
|
351
|
+
}
|
|
352
|
+
const terminal = await this.startTerminalAsync(shellId, folder, params.size, null, params.storedLimit, params.notice, false, null, client, randomUUID(), params.environment, false, params.opensTab);
|
|
353
|
+
if (!params.opensTab)
|
|
354
|
+
terminal.launcher = client;
|
|
355
|
+
if (params.command !== null)
|
|
356
|
+
void this.typeCommandAsync(terminal, params.command).then(t => {
|
|
357
|
+
terminal.launchCommandOffset = terminal.session.text.endOffset;
|
|
358
|
+
if (t !== TerminalRunOutcome.Typed || !terminal.session.commands.hasMarks || terminal.session.commands.promptEndOffset === null || terminal.keyCount !== 0)
|
|
359
|
+
this.endLaunchCommand(terminal, null);
|
|
360
|
+
}).catch((error) => this.log.write(Resources.formatLaunchCommandFailed(terminal.session.sessionId, FailureText.read(error))));
|
|
361
|
+
return new TerminalLaunched(terminal.session.sessionId, null);
|
|
362
|
+
}
|
|
363
|
+
async typeCommandAsync(terminal, command, isPaste = false, waitsForFit = false) {
|
|
364
|
+
const session = terminal.session;
|
|
365
|
+
const typed = isPaste ? command.replace(Resources.leadingBlankLines, "") : command;
|
|
366
|
+
const pending = new AbortController();
|
|
367
|
+
terminal.pendingCommand = pending;
|
|
368
|
+
const signal = pending.signal;
|
|
369
|
+
const inputCount = terminal.inputCount;
|
|
370
|
+
const keyCount = terminal.keyCount;
|
|
371
|
+
if (waitsForFit)
|
|
372
|
+
await session.changes.waitUntilAsync(() => session.hasExited || terminal.isSized || terminal.deferredShell?.hasStarted !== false, null, signal);
|
|
373
|
+
const startedAt = Date.now();
|
|
374
|
+
const isReady = () => session.hasIntegration ? session.commands.promptEndOffset !== null : session.receivedLength > 0;
|
|
375
|
+
const isStarted = () => session.hasExited || isReady() || session.commands.isLoaded;
|
|
376
|
+
if (await session.waitForStartAsync(isStarted, signal) && session.text.endOffset > 0)
|
|
377
|
+
await session.changes.waitUntilAsync(isStarted, Resources.integrationWaitMilliseconds, signal);
|
|
378
|
+
const deadline = startedAt + Resources.integrationWaitMilliseconds;
|
|
379
|
+
while (!session.hasIntegration && !session.hasExited && Date.now() < deadline) {
|
|
380
|
+
const length = session.receivedLength;
|
|
381
|
+
await session.changes.waitUntilAsync(() => session.hasExited || session.receivedLength !== length, Resources.promptSettleMilliseconds, signal);
|
|
382
|
+
if (session.receivedLength === length)
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
if (session.hasIntegration && session.commands.isLoaded)
|
|
386
|
+
await session.changes.waitUntilAsync(() => session.hasExited || isReady(), Resources.shellStartMilliseconds, signal);
|
|
387
|
+
const isEnded = () => signal.aborted || session.hasExited || this.sessions.get(session.sessionId) !== terminal;
|
|
388
|
+
const isBracketed = isPaste && !isEnded() && await session.isBracketedPasteAsync();
|
|
389
|
+
const outcome = isEnded() ? TerminalRunOutcome.Ended
|
|
390
|
+
: isPaste && ((waitsForFit ? terminal.keyCount !== keyCount : terminal.inputCount !== inputCount) || session.commands.promptEndOffset !== null && session.readPromptLine() !== "") ? TerminalRunOutcome.Input
|
|
391
|
+
: isPaste && !isBracketed && typed.includes(Resources.tabKey) ? TerminalRunOutcome.Tab
|
|
392
|
+
: TerminalRunOutcome.Typed;
|
|
393
|
+
if (outcome === TerminalRunOutcome.Typed) {
|
|
394
|
+
const text = isPaste ? TerminalHost.toPaste(typed, isBracketed) : `${command}${Resources.enterKey}`;
|
|
395
|
+
terminal.typedEnds = isBracketed || !isPaste ? 1 : text.split(Resources.enterKey).length - 1;
|
|
396
|
+
terminal.paste = isBracketed ? typed.replace(Resources.whitespace, "") : "";
|
|
397
|
+
if (isPaste)
|
|
398
|
+
terminal.recordRunInput();
|
|
399
|
+
session.write(text);
|
|
400
|
+
}
|
|
401
|
+
if (terminal.pendingCommand === pending)
|
|
402
|
+
terminal.pendingCommand = null;
|
|
403
|
+
return outcome;
|
|
404
|
+
}
|
|
405
|
+
endLaunchCommand(terminal, exitCode) {
|
|
406
|
+
if (terminal.launchCommandOffset === null)
|
|
407
|
+
return;
|
|
408
|
+
terminal.launchCommandOffset = null;
|
|
409
|
+
this.audience.publish(this.events.launchCommandEnded, terminal, new TerminalCommandEnded(terminal.session.sessionId, exitCode).toJson());
|
|
410
|
+
}
|
|
411
|
+
static findEchoEnd(output, paste) {
|
|
412
|
+
const start = output.replace(Resources.whitespace, "").indexOf(paste);
|
|
413
|
+
if (start < 0)
|
|
414
|
+
return null;
|
|
415
|
+
let index = 0;
|
|
416
|
+
for (let count = 0; count < start + paste.length; index++)
|
|
417
|
+
if (output.charAt(index).trim().length > 0)
|
|
418
|
+
count++;
|
|
419
|
+
return index;
|
|
420
|
+
}
|
|
421
|
+
static toPaste(command, isBracketed) {
|
|
422
|
+
const text = command.replace(Resources.trailingLineBreaks, "").replace(Resources.pasteLineBreaks, Resources.enterKey);
|
|
423
|
+
return isBracketed ? `${Resources.pasteStart}${text}${Resources.pasteEnd}${Resources.enterKey}` : `${text}${Resources.enterKey}`;
|
|
424
|
+
}
|
|
425
|
+
async runAsync(params, client) {
|
|
426
|
+
await Promise.allSettled([this.restoring]);
|
|
427
|
+
this.ensureRunning();
|
|
428
|
+
if (params.sessionId !== null) {
|
|
429
|
+
const target = this.findRunTarget(params.sessionId, params, client);
|
|
430
|
+
return target === null ? new TerminalRunResult(params.sessionId, TerminalRunOutcome.Ended) : this.runIntoAsync(params, target, client);
|
|
431
|
+
}
|
|
432
|
+
let terminal = this.terminals.findLast(t => t.owner === params.owner && t.isReachedBy(client.connection, client.isApp)
|
|
433
|
+
&& (params.shellKinds.length === 0 || params.shellKinds.includes(t.shell.kind)) && t.isFree && !t.isRunStarting);
|
|
434
|
+
const isNew = terminal === undefined;
|
|
435
|
+
if (terminal === undefined) {
|
|
436
|
+
const shell = params.shellKinds.length === 0 ? await this.findFallbackShellAsync(params.shellId)
|
|
437
|
+
: await this.findShellOfKindsAsync(params.shellKinds, params.shellId);
|
|
438
|
+
if (shell === undefined && params.shellKinds.length > 0)
|
|
439
|
+
return new TerminalRunResult(null, TerminalRunOutcome.NoShell);
|
|
440
|
+
terminal = await this.startForPersonAsync(new TerminalOpenParams(shell?.id ?? null, params.folder, params.size, params.storedLimit, false, params.owner), client, true, params.opensTab);
|
|
441
|
+
if (!params.opensTab)
|
|
442
|
+
terminal.fitWindow = client;
|
|
443
|
+
this.audience.publish(this.events.opened, terminal, terminal.session.toSummary(terminal.folder, false, false, params.owner, terminal.opensTab, terminal.name, terminal.shell.name).toJson());
|
|
444
|
+
this.startIfUnshown(terminal);
|
|
445
|
+
}
|
|
446
|
+
if (!params.opensTab)
|
|
447
|
+
this.events.runOpened.publishTo(client.connection, client.window, new TerminalReference(terminal.session.sessionId).toJson());
|
|
448
|
+
return this.typeRunAsync(terminal, params.command, client, isNew);
|
|
449
|
+
}
|
|
450
|
+
findRunTarget(sessionId, params, client) {
|
|
451
|
+
const terminal = this.sessions.get(sessionId);
|
|
452
|
+
return terminal === undefined || !terminal.isReachedBy(client.connection, client.isApp) || terminal.ownerSessionId !== null
|
|
453
|
+
|| terminal.owner !== params.owner || params.shellKinds.length > 0 && !params.shellKinds.includes(terminal.shell.kind) ? null : terminal;
|
|
454
|
+
}
|
|
455
|
+
async runIntoAsync(params, terminal, client) {
|
|
456
|
+
const session = terminal.session;
|
|
457
|
+
if (terminal.isRunStarting)
|
|
458
|
+
return new TerminalRunResult(session.sessionId, TerminalRunOutcome.Busy);
|
|
459
|
+
terminal.isRunStarting = true;
|
|
460
|
+
try {
|
|
461
|
+
const isBusy = () => !terminal.isFree && !session.hasExited && this.sessions.get(session.sessionId) === terminal;
|
|
462
|
+
if (isBusy() && params.whenBusy === TerminalRunBusy.Interrupt && terminal.pendingCommand === null) {
|
|
463
|
+
const isStarted = () => isBusy() && session.commands.runningCommand !== null;
|
|
464
|
+
const signal = terminal.beginInterrupt();
|
|
465
|
+
const deadline = Date.now() + Resources.runInterruptWaitMilliseconds;
|
|
466
|
+
await session.changes.waitUntilAsync(() => !isBusy() || isStarted(), Resources.runInterruptWaitMilliseconds, signal);
|
|
467
|
+
if (isStarted() && await TerminalHost.waitForCommandAsync(session, isStarted, deadline, signal)) {
|
|
468
|
+
session.write(Resources.interruptKey);
|
|
469
|
+
await session.changes.waitUntilAsync(() => !isBusy(), Resources.runInterruptWaitMilliseconds, signal);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (isBusy())
|
|
473
|
+
return new TerminalRunResult(session.sessionId, TerminalRunOutcome.Busy);
|
|
474
|
+
return await this.typeRunAsync(terminal, params.command, client);
|
|
475
|
+
}
|
|
476
|
+
finally {
|
|
477
|
+
terminal.isRunStarting = false;
|
|
478
|
+
terminal.endInterrupt();
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
static readWindowsBuild(osRelease) {
|
|
482
|
+
const build = SystemRequirements.readWindowsBuild(osRelease);
|
|
483
|
+
if (build === null)
|
|
484
|
+
throw new UnsupportedSystemException(SystemRequirement.WindowsBuild, osRelease);
|
|
485
|
+
return build;
|
|
486
|
+
}
|
|
487
|
+
static async waitForCommandAsync(session, isStarted, deadline, signal) {
|
|
488
|
+
for (;;) {
|
|
489
|
+
const group = await session.readForegroundProcessGroupAsync();
|
|
490
|
+
if (!isStarted() || signal.aborted)
|
|
491
|
+
return false;
|
|
492
|
+
if (group !== session.pid)
|
|
493
|
+
return true;
|
|
494
|
+
if (Date.now() >= deadline)
|
|
495
|
+
return false;
|
|
496
|
+
await session.changes.waitUntilAsync(() => !isStarted(), Math.min(Resources.foregroundPollMilliseconds, deadline - Date.now()), signal);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
async typeRunAsync(terminal, command, client, waitsForFit = false) {
|
|
500
|
+
const session = terminal.session;
|
|
501
|
+
const outcome = await this.typeCommandAsync(terminal, command, true, waitsForFit);
|
|
502
|
+
if (outcome !== TerminalRunOutcome.Typed)
|
|
503
|
+
return new TerminalRunResult(session.sessionId, outcome);
|
|
504
|
+
const runId = randomUUID();
|
|
505
|
+
terminal.addRun(runId, session.anchorRunAsync());
|
|
506
|
+
const ended = [];
|
|
507
|
+
const previous = this.endRun(terminal, TerminalRunEndedBy.Command, terminal.waitingRun?.exitCode ?? null, client);
|
|
508
|
+
if (previous !== null)
|
|
509
|
+
ended.push(previous);
|
|
510
|
+
if (session.commands.hasMarks && session.commands.promptEndOffset !== null)
|
|
511
|
+
terminal.waitingRun = new WaitingRun(runId, client.connection, client.window, terminal.typedEnds, session.text.endOffset, terminal.paste);
|
|
512
|
+
else
|
|
513
|
+
ended.push(new TerminalRunEnded(session.sessionId, runId, TerminalRunEndedBy.NoIntegration, null));
|
|
514
|
+
return new TerminalRunResult(session.sessionId, outcome, runId, ended);
|
|
515
|
+
}
|
|
516
|
+
countRunEnd(terminal, exitCode, isEndMark) {
|
|
517
|
+
const run = terminal.waitingRun;
|
|
518
|
+
if (run === null)
|
|
519
|
+
return;
|
|
520
|
+
const text = terminal.session.text;
|
|
521
|
+
const output = text.readRange(run.offset, text.endOffset);
|
|
522
|
+
const echoEnd = run.paste.length === 0 ? 0 : TerminalHost.findEchoEnd(output, run.paste);
|
|
523
|
+
if (!isEndMark && (run.isAfterEndMark || echoEnd === null || !output.slice(echoEnd).includes(Resources.lineFeed))) {
|
|
524
|
+
run.isAfterEndMark = false;
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
run.isAfterEndMark = isEndMark;
|
|
528
|
+
run.exitCode = exitCode;
|
|
529
|
+
run.offset = text.endOffset;
|
|
530
|
+
run.paste = "";
|
|
531
|
+
run.remainingEnds--;
|
|
532
|
+
if (run.remainingEnds === 0)
|
|
533
|
+
this.endRun(terminal, TerminalRunEndedBy.Command, exitCode);
|
|
534
|
+
}
|
|
535
|
+
endRun(terminal, endedBy, exitCode, answered = null) {
|
|
536
|
+
const run = terminal.waitingRun;
|
|
537
|
+
if (run === null)
|
|
538
|
+
return null;
|
|
539
|
+
terminal.waitingRun = null;
|
|
540
|
+
const ended = new TerminalRunEnded(terminal.session.sessionId, run.runId, endedBy, endedBy === TerminalRunEndedBy.Command ? exitCode : null);
|
|
541
|
+
if (answered !== null && answered.connection === run.connection && answered.window === run.window)
|
|
542
|
+
return ended;
|
|
543
|
+
this.events.runEnded.publishTo(run.connection, run.window, ended.toJson());
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
async readRunOutputAsync(params, client) {
|
|
547
|
+
const terminal = this.sessions.get(params.sessionId);
|
|
548
|
+
const runs = terminal === undefined ? [] : [...terminal.runs];
|
|
549
|
+
const index = runs.findIndex(([runId]) => runId === params.runId);
|
|
550
|
+
const run = runs[index];
|
|
551
|
+
if (terminal === undefined || run === undefined || !client.isApp || !terminal.isReachedBy(client.connection, true) || terminal.ownerSessionId !== null
|
|
552
|
+
|| terminal.session.hasExited || terminal.isRestarting)
|
|
553
|
+
return null;
|
|
554
|
+
const next = runs[index + 1];
|
|
555
|
+
return terminal.session.readRunLinesAsync(await run[1], next === undefined ? null : await next[1], params.maximumLines);
|
|
556
|
+
}
|
|
557
|
+
async findFallbackShellAsync(shellId) {
|
|
558
|
+
return (shellId === null ? undefined : await this.locator.findAsync(shellId)) ?? await this.locator.findAsync(null);
|
|
559
|
+
}
|
|
560
|
+
async findShellOfKindsAsync(kinds, shellId) {
|
|
561
|
+
const fallback = await this.findFallbackShellAsync(shellId);
|
|
562
|
+
return fallback !== undefined && kinds.includes(fallback.kind) ? fallback : await this.locator.findKindAsync(kinds);
|
|
563
|
+
}
|
|
564
|
+
openAsync(params, client) {
|
|
565
|
+
return this.trackAsync(this.openRequestedAsync(params, client));
|
|
566
|
+
}
|
|
567
|
+
async openRequestedAsync(params, client) {
|
|
568
|
+
if (!params.isPanelTerminal)
|
|
569
|
+
return this.openForPersonAsync(params, client);
|
|
570
|
+
await Promise.allSettled([this.restoring]);
|
|
571
|
+
const panel = this.terminals.findLast(t => t.isPanelTerminal && t.isReachedBy(client.connection, client.isApp));
|
|
572
|
+
if (panel !== undefined)
|
|
573
|
+
return new TerminalReference(panel.session.sessionId);
|
|
574
|
+
let opening = this.panelOpenings.get(client.connection);
|
|
575
|
+
if (opening === undefined) {
|
|
576
|
+
opening = this.openForPersonAsync(params, client).finally(() => {
|
|
577
|
+
this.panelOpenings.delete(client.connection);
|
|
578
|
+
});
|
|
579
|
+
this.panelOpenings.set(client.connection, opening);
|
|
580
|
+
}
|
|
581
|
+
return opening;
|
|
582
|
+
}
|
|
583
|
+
async openForPersonAsync(params, client) {
|
|
584
|
+
return new TerminalReference((await this.startForPersonAsync(params, client)).session.sessionId);
|
|
585
|
+
}
|
|
586
|
+
async startForPersonAsync(params, client, startsAtFit = false, opensTab = true) {
|
|
587
|
+
if (params.folder !== null && !this.isAbsolute(params.folder))
|
|
588
|
+
throw new ArgumentException(ProtocolResources.formatNotAbsolutePath(ProtocolResources.valueSubject), Resources.folderParameter);
|
|
589
|
+
const missing = params.folder !== null && !(await this.fileSystem.isFolderAsync(params.folder)) ? params.folder : null;
|
|
590
|
+
this.ensureRunning();
|
|
591
|
+
const folder = params.folder === null || missing !== null ? this.homeFolder : params.folder;
|
|
592
|
+
const terminal = await this.openTerminalAsync(params.shellId, folder, params.size, null, params.storedLimit, missing === null ? null : Resources.formatStartFolderNote(missing), params.isPanelTerminal, params.owner, client, randomUUID(), null, startsAtFit, opensTab);
|
|
593
|
+
if (missing !== null)
|
|
594
|
+
this.log.write(Resources.formatStartFolderMissing(terminal.session.sessionId, missing, this.homeFolder));
|
|
595
|
+
return terminal;
|
|
596
|
+
}
|
|
597
|
+
async restoreAsync(params, client) {
|
|
598
|
+
if (!client.isApp)
|
|
599
|
+
return;
|
|
600
|
+
if (this.appCloses.size > 0) {
|
|
601
|
+
await Promise.allSettled(this.appCloses);
|
|
602
|
+
if (!this.audience.isApp(client.connection))
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
this.restoring ??= this.restoreSavedAsync(params, client);
|
|
606
|
+
await this.restoring;
|
|
607
|
+
}
|
|
608
|
+
async restoreSavedAsync(params, client) {
|
|
609
|
+
let saved;
|
|
610
|
+
try {
|
|
611
|
+
saved = this.saved.list();
|
|
612
|
+
}
|
|
613
|
+
catch (error) {
|
|
614
|
+
this.log.write(Resources.formatSavedTerminalsUnreadable(FailureText.read(error)));
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
const panel = saved.findLast(t => t.isPanelTerminal);
|
|
618
|
+
const restoring = [];
|
|
619
|
+
for (const terminal of saved) {
|
|
620
|
+
if (this.sessions.has(terminal.sessionId))
|
|
621
|
+
continue;
|
|
622
|
+
if (terminal.isPanelTerminal && (terminal !== panel || this.panelOpenings.has(client.connection)
|
|
623
|
+
|| this.terminals.some(t => t.isPanelTerminal && t.connection === client.connection)))
|
|
624
|
+
this.saved.forget(terminal.sessionId);
|
|
625
|
+
else
|
|
626
|
+
restoring.push(terminal);
|
|
627
|
+
}
|
|
628
|
+
await Promise.all(restoring.map(t => this.restoreOrForgetAsync(t, params, client)));
|
|
629
|
+
for (const terminal of restoring) {
|
|
630
|
+
const hosted = this.sessions.get(terminal.sessionId);
|
|
631
|
+
if (hosted !== undefined) {
|
|
632
|
+
this.sessions.delete(terminal.sessionId);
|
|
633
|
+
this.sessions.set(terminal.sessionId, hosted);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
async restoreOrForgetAsync(saved, params, client) {
|
|
638
|
+
try {
|
|
639
|
+
await this.restoreTerminalAsync(saved, params, client);
|
|
640
|
+
}
|
|
641
|
+
catch (error) {
|
|
642
|
+
this.ensureRunning();
|
|
643
|
+
this.saved.forget(saved.sessionId);
|
|
644
|
+
this.log.write(Resources.formatRestoreFailed(saved.sessionId, FailureText.read(error)));
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
async restoreTerminalAsync(saved, params, client) {
|
|
648
|
+
const hasShell = await this.locator.findAsync(saved.shellId) !== undefined;
|
|
649
|
+
const fallback = params.shellId !== null && await this.locator.findAsync(params.shellId) !== undefined ? params.shellId : null;
|
|
650
|
+
const hasFolder = this.isAbsolute(saved.folder) && await this.fileSystem.isFolderAsync(saved.folder);
|
|
651
|
+
this.ensureRunning();
|
|
652
|
+
const note = hasShell
|
|
653
|
+
? hasFolder ? null : Resources.formatStartFolderNote(saved.folder)
|
|
654
|
+
: hasFolder ? Resources.formatRestoredShellNote(saved.shellId) : Resources.formatRestoredShellAndFolderNote(saved.shellId, saved.folder);
|
|
655
|
+
const terminal = await this.openTerminalAsync(hasShell ? saved.shellId : fallback, hasFolder ? saved.folder : this.homeFolder, params.size, null, params.storedLimit, note, saved.isPanelTerminal, saved.owner, client, saved.sessionId, null, false, saved.opensTab);
|
|
656
|
+
const name = saved.name;
|
|
657
|
+
terminal.name = name !== null && name.trim() !== "" && name.length <= ProtocolResources.maximumTitleLength ? name : null;
|
|
658
|
+
if (!hasShell)
|
|
659
|
+
this.log.write(Resources.formatRestoredShellMissing(saved.sessionId, saved.shellId));
|
|
660
|
+
if (!hasFolder)
|
|
661
|
+
this.log.write(Resources.formatStartFolderMissing(saved.sessionId, saved.folder, this.homeFolder));
|
|
662
|
+
}
|
|
663
|
+
toVariableKey(name) {
|
|
664
|
+
return this.windowsPty === null ? name : name.toUpperCase();
|
|
665
|
+
}
|
|
666
|
+
isAbsolute(folder) {
|
|
667
|
+
return this.windowsPty === null ? folder.startsWith(Resources.posixRoot) : Resources.windowsAbsolutePath.test(folder);
|
|
668
|
+
}
|
|
669
|
+
async closeAsync(sessionId, client) {
|
|
670
|
+
this.find(sessionId, client);
|
|
671
|
+
await this.closeTerminalAsync(sessionId);
|
|
672
|
+
}
|
|
673
|
+
rename(name, client) {
|
|
674
|
+
const terminal = this.findHosted(name.sessionId, client);
|
|
675
|
+
terminal.name = name.name;
|
|
676
|
+
this.saved.rename(name.sessionId, name.name);
|
|
677
|
+
this.audience.publish(this.events.renamed, terminal, name.toJson());
|
|
678
|
+
}
|
|
679
|
+
async completeAsync(params, client) {
|
|
680
|
+
const terminal = this.findHosted(params.sessionId, client);
|
|
681
|
+
const keys = params.asksShell && this.completions.followsCommand(terminal.shell.kind, params.line, params.cursor)
|
|
682
|
+
? Resources.optionsKeys.get(terminal.shell.kind) : undefined;
|
|
683
|
+
const [completion, reply] = await Promise.all([
|
|
684
|
+
this.readCompletionEnvironmentAsync(terminal).then(t => this.completions.completeAsync(params.sessionId, terminal.shell.kind, params.line, params.cursor, terminal.session.cwd ?? terminal.folder, t, terminal.session.shellNames, terminal.session.shellBuiltins)),
|
|
685
|
+
keys === undefined ? Promise.resolve(ShellOptionsReply.none) : terminal.session.askOptionsAsync(keys)
|
|
686
|
+
]);
|
|
687
|
+
if (reply.isTimedOut)
|
|
688
|
+
return new TerminalCompletion(params.cursor, [], true);
|
|
689
|
+
return reply.options === null ? completion
|
|
690
|
+
: this.completions.addOptions(completion, terminal.shell.kind, params.line, params.cursor, reply.options);
|
|
691
|
+
}
|
|
692
|
+
async findCommandsAsync(params, client) {
|
|
693
|
+
const terminal = this.findHosted(params.sessionId, client);
|
|
694
|
+
const shellNames = terminal.session.isShellPathReset || terminal.session.isShellModulePathReset ? null
|
|
695
|
+
: terminal.session.shellNames;
|
|
696
|
+
const environment = await this.readCompletionEnvironmentAsync(terminal);
|
|
697
|
+
return terminal.shell.kind === TerminalShellKind.PowerShell
|
|
698
|
+
? this.completions.findMissingPowerShellAsync(terminal.session.commands, terminal.shell.file, params.names, shellNames, environment)
|
|
699
|
+
: this.completions.findMissingAsync(params.sessionId, terminal.session.commands, terminal.session.prompts, terminal.shell.kind, terminal.session.cwd ?? terminal.folder, params.names, shellNames, environment);
|
|
700
|
+
}
|
|
701
|
+
async readCompletionEnvironmentAsync(terminal) {
|
|
702
|
+
const replaced = {};
|
|
703
|
+
if (terminal.session.shellPath !== null)
|
|
704
|
+
replaced[Resources.pathVariable] = terminal.session.shellPath;
|
|
705
|
+
if (terminal.session.shellModulePath !== null)
|
|
706
|
+
replaced[Resources.modulePathVariable] = terminal.session.shellModulePath;
|
|
707
|
+
const temporaryFolder = terminal.session.shellTemporaryFolder ?? terminal.startTemporaryFolder;
|
|
708
|
+
if (temporaryFolder !== null)
|
|
709
|
+
replaced[Resources.temporaryFolderVariable] = temporaryFolder;
|
|
710
|
+
const keys = new Set(Object.keys(replaced).map(t => this.toVariableKey(t)));
|
|
711
|
+
return this.readShellEnvironmentAsync({
|
|
712
|
+
...Object.fromEntries(Object.entries(terminal.environment ?? {}).filter(([t]) => !keys.has(this.toVariableKey(t)))),
|
|
713
|
+
...replaced
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
async restartAsync(params, client) {
|
|
717
|
+
const sessionId = params.sessionId;
|
|
718
|
+
const terminal = this.findHosted(sessionId, client);
|
|
719
|
+
if (terminal.ownerSessionId !== null)
|
|
720
|
+
throw new AgentTerminalRestartException(sessionId);
|
|
721
|
+
const restart = terminal.restarts.then(() => this.restartNowAsync(sessionId, terminal, params.size));
|
|
722
|
+
terminal.restarts = restart.then(() => undefined, () => undefined);
|
|
723
|
+
await restart;
|
|
724
|
+
}
|
|
725
|
+
async restartNowAsync(sessionId, terminal, size) {
|
|
726
|
+
if (this.sessions.get(sessionId) !== terminal)
|
|
727
|
+
throw new SessionNotFoundException(sessionId);
|
|
728
|
+
terminal.cancelCommand();
|
|
729
|
+
this.endLaunchCommand(terminal, null);
|
|
730
|
+
this.endRun(terminal, TerminalRunEndedBy.TerminalEnded, null);
|
|
731
|
+
terminal.isRestarting = true;
|
|
732
|
+
try {
|
|
733
|
+
const session = terminal.session;
|
|
734
|
+
if (!session.hasExited && !await session.endAsync())
|
|
735
|
+
this.log.write(Resources.formatShellLeftRunning(sessionId, session.pid));
|
|
736
|
+
const started = await this.startShellAsync(terminal.shell, terminal.folder, size, null, terminal.environment ?? {});
|
|
737
|
+
if (this.sessions.get(sessionId) !== terminal) {
|
|
738
|
+
if (!await started.process.endAsync())
|
|
739
|
+
this.log.write(Resources.formatLateShellLeftRunning(started.process.pid));
|
|
740
|
+
throw new SessionNotFoundException(sessionId);
|
|
741
|
+
}
|
|
742
|
+
session.restart(started.process, started.nonce, size);
|
|
743
|
+
terminal.startTemporaryFolder = started.temporaryFolder;
|
|
744
|
+
terminal.clearRuns();
|
|
745
|
+
terminal.heldExit = null;
|
|
746
|
+
}
|
|
747
|
+
finally {
|
|
748
|
+
terminal.isRestarting = false;
|
|
749
|
+
this.sendHeldExit(sessionId, terminal);
|
|
750
|
+
}
|
|
751
|
+
this.audience.publish(this.events.restarted, terminal, new TerminalReference(sessionId).toJson());
|
|
752
|
+
}
|
|
753
|
+
sendHeldExit(sessionId, terminal) {
|
|
754
|
+
const exited = terminal.heldExit;
|
|
755
|
+
terminal.heldExit = null;
|
|
756
|
+
if (exited === null || this.sessions.get(sessionId) !== terminal)
|
|
757
|
+
return;
|
|
758
|
+
this.audience.publish(this.events.exited, terminal, exited.toJson());
|
|
759
|
+
this.forgetOldestExited();
|
|
760
|
+
}
|
|
761
|
+
forgetOldestExited() {
|
|
762
|
+
const exited = this.terminals.filter(t => t.session.hasExited && !t.isRestarting);
|
|
763
|
+
for (const terminal of exited.slice(0, Math.max(exited.length - Resources.maximumExitedSessions, 0))) {
|
|
764
|
+
this.sessions.delete(terminal.session.sessionId);
|
|
765
|
+
this.saved.forget(terminal.session.sessionId);
|
|
766
|
+
const disposal = this.disposeAsync(terminal.session);
|
|
767
|
+
this.disposals.add(disposal);
|
|
768
|
+
void disposal.then(() => this.disposals.delete(disposal), (error) => {
|
|
769
|
+
this.disposals.delete(disposal);
|
|
770
|
+
this.log.write(Resources.formatEndedTerminalDisposalFailed(terminal.session.sessionId, FailureText.read(error)));
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
readLinesAsync(params, client) {
|
|
775
|
+
return this.find(params.sessionId, client).readLinesAsync(params.start, params.limit);
|
|
776
|
+
}
|
|
777
|
+
addCommand(shellId, command) {
|
|
778
|
+
if (!this.isSuggesting())
|
|
779
|
+
return;
|
|
780
|
+
this.commandHistory.add(shellId, command);
|
|
781
|
+
for (const terminal of this.sessions.values())
|
|
782
|
+
if (terminal.shell.id === shellId && terminal.ownerSessionId === null)
|
|
783
|
+
this.audience.publish(this.events.commandAdded, terminal, new TerminalCommandAdded(terminal.session.sessionId, command).toJson());
|
|
784
|
+
}
|
|
785
|
+
attachAsync(reference, client) {
|
|
786
|
+
const terminal = this.findHosted(reference.sessionId, client);
|
|
787
|
+
return terminal.session.attachAsync(reference.paneId, terminal.ownerSessionId === null && this.isSuggesting() ? this.commandHistory.list(terminal.shell.id) : []);
|
|
788
|
+
}
|
|
789
|
+
isSuggesting() {
|
|
790
|
+
return this.integration.isSuggesting();
|
|
791
|
+
}
|
|
792
|
+
input(params, client) {
|
|
793
|
+
const terminal = this.findHosted(params.sessionId, client);
|
|
794
|
+
const isReply = Resources.terminalReplies.test(params.data);
|
|
795
|
+
terminal.recordInput(isReply, params.data);
|
|
796
|
+
if (!isReply)
|
|
797
|
+
terminal.keyCount++;
|
|
798
|
+
terminal.session.write(params.data, !isReply);
|
|
799
|
+
}
|
|
800
|
+
resize(params, client) {
|
|
801
|
+
const terminal = this.findHosted(params.sessionId, client);
|
|
802
|
+
if (!terminal.session.resize(params.size))
|
|
803
|
+
terminal.deferredShell?.start();
|
|
804
|
+
terminal.isSized = true;
|
|
805
|
+
terminal.session.changes.notify();
|
|
806
|
+
}
|
|
807
|
+
acknowledge(params, client) {
|
|
808
|
+
this.reachable(params.sessionId, client)?.session.acknowledge(params.paneId, params.offset);
|
|
809
|
+
}
|
|
810
|
+
setVisible(params, client) {
|
|
811
|
+
this.find(params.sessionId, client).setVisible(params.paneId, params.isVisible);
|
|
812
|
+
}
|
|
813
|
+
detach(reference, client) {
|
|
814
|
+
const terminal = this.reachable(reference.sessionId, client);
|
|
815
|
+
terminal?.session.detach(reference.paneId);
|
|
816
|
+
if (terminal !== null && !terminal.isSized)
|
|
817
|
+
this.startShell(terminal);
|
|
818
|
+
}
|
|
819
|
+
startUnshownShells() {
|
|
820
|
+
for (const terminal of this.terminals)
|
|
821
|
+
this.startIfUnshown(terminal);
|
|
822
|
+
}
|
|
823
|
+
startIfUnshown(terminal) {
|
|
824
|
+
if (terminal.isSized)
|
|
825
|
+
return;
|
|
826
|
+
const fitWindow = terminal.fitWindow;
|
|
827
|
+
if (fitWindow === null ? !terminal.opensTab || !this.audience.shows(terminal) : !this.audience.isOpen(fitWindow) || this.audience.isHidden(fitWindow))
|
|
828
|
+
this.startShell(terminal);
|
|
829
|
+
}
|
|
830
|
+
startShell(terminal) {
|
|
831
|
+
if (terminal.deferredShell === null || terminal.deferredShell.hasStarted)
|
|
832
|
+
return;
|
|
833
|
+
terminal.deferredShell.start();
|
|
834
|
+
terminal.session.changes.notify();
|
|
835
|
+
}
|
|
836
|
+
find(sessionId, client) {
|
|
837
|
+
return this.findHosted(sessionId, client).session;
|
|
838
|
+
}
|
|
839
|
+
findHosted(sessionId, client) {
|
|
840
|
+
const terminal = this.reachable(sessionId, client);
|
|
841
|
+
if (terminal === null)
|
|
842
|
+
throw new SessionNotFoundException(sessionId);
|
|
843
|
+
return terminal;
|
|
844
|
+
}
|
|
845
|
+
reachable(sessionId, client) {
|
|
846
|
+
const terminal = this.sessions.get(sessionId);
|
|
847
|
+
return terminal === undefined || !terminal.isReachedBy(client.connection, client.isApp) ? null : terminal;
|
|
848
|
+
}
|
|
849
|
+
async endAsync(session) {
|
|
850
|
+
await this.stopAsync(session);
|
|
851
|
+
await this.disposeAsync(session);
|
|
852
|
+
}
|
|
853
|
+
async stopAsync(session) {
|
|
854
|
+
const hasEnded = await session.endAsync();
|
|
855
|
+
if (!hasEnded)
|
|
856
|
+
this.log.write(Resources.formatShellLeftRunning(session.sessionId, session.pid));
|
|
857
|
+
return hasEnded;
|
|
858
|
+
}
|
|
859
|
+
async disposeAsync(session) {
|
|
860
|
+
for (const failure of await session.disposeAsync())
|
|
861
|
+
this.log.write(Resources.formatStoredLinesRemovalFailed(session.sessionId, FailureText.read(failure)));
|
|
862
|
+
}
|
|
863
|
+
ensureRunning() {
|
|
864
|
+
if (this.hasEnded)
|
|
865
|
+
throw new HostEndedException();
|
|
866
|
+
}
|
|
867
|
+
async spawnFromAsync(base, file, args, folder, size, environment, inheritCursor) {
|
|
868
|
+
const process = await this.spawner.spawnAsync(file, args, folder, size.columns, size.rows, { ...base, ...environment }, inheritCursor);
|
|
869
|
+
if (this.hasEnded) {
|
|
870
|
+
if (!await process.endAsync())
|
|
871
|
+
this.log.write(Resources.formatLateShellLeftRunning(process.pid));
|
|
872
|
+
throw new HostEndedException();
|
|
873
|
+
}
|
|
874
|
+
return process;
|
|
875
|
+
}
|
|
876
|
+
async startShellAsync(shell, folder, size, note, variables, deferredSessionId = null) {
|
|
877
|
+
const environment = await this.readShellEnvironmentAsync(variables);
|
|
878
|
+
const printedNote = note === null || this.windowsPty === null ? null
|
|
879
|
+
: note.split(Resources.lineBreaks).map(t => TerminalSession.toNote(t)).join("");
|
|
880
|
+
const launch = await this.integration.prepareAsync(shell, randomUUID().replaceAll(Resources.terminalHostUuidSeparator, ""), environment, printedNote);
|
|
881
|
+
const inheritsCursor = note !== null && !launch.printsNote;
|
|
882
|
+
let process;
|
|
883
|
+
let deferred = null;
|
|
884
|
+
try {
|
|
885
|
+
this.ensureRunning();
|
|
886
|
+
if (deferredSessionId === null)
|
|
887
|
+
process = await this.spawnFromAsync(environment, launch.file, launch.args, folder, size, launch.environment, inheritsCursor);
|
|
888
|
+
else {
|
|
889
|
+
deferred = new DeferredPtyProcess(async (columns, rows) => {
|
|
890
|
+
try {
|
|
891
|
+
return await this.spawnFromAsync(environment, launch.file, launch.args, folder, new TerminalSize(columns, rows), launch.environment, inheritsCursor);
|
|
892
|
+
}
|
|
893
|
+
catch (error) {
|
|
894
|
+
this.log.write(Resources.formatDeferredShellNotStarted(deferredSessionId, FailureText.read(error)));
|
|
895
|
+
throw error;
|
|
896
|
+
}
|
|
897
|
+
}, size.columns, size.rows);
|
|
898
|
+
process = deferred;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
catch (error) {
|
|
902
|
+
await this.integration.removeNonceFileAsync(launch.nonceFile);
|
|
903
|
+
throw error;
|
|
904
|
+
}
|
|
905
|
+
this.integration.watch(process, launch);
|
|
906
|
+
const temporaryKey = this.toVariableKey(Resources.temporaryFolderVariable);
|
|
907
|
+
return new StartedShell(process, launch.nonce, launch.printsNote, deferred, Object.entries(environment).find(([t]) => this.toVariableKey(t) === temporaryKey)?.[1] ?? null);
|
|
908
|
+
}
|
|
909
|
+
async readShellEnvironmentAsync(variables) {
|
|
910
|
+
const names = new Set(Object.keys(variables).map(t => this.toVariableKey(t)));
|
|
911
|
+
return Object.fromEntries([
|
|
912
|
+
...Object.entries(await this.readEnvironmentAsync()).filter(([t]) => !names.has(this.toVariableKey(t))),
|
|
913
|
+
...Object.entries(variables)
|
|
914
|
+
]);
|
|
915
|
+
}
|
|
916
|
+
async readEnvironmentAsync() {
|
|
917
|
+
const environment = await this.environmentSource.readAsync();
|
|
918
|
+
this.ensureRunning();
|
|
919
|
+
return TerminalHost.toTerminalEnvironment(environment);
|
|
920
|
+
}
|
|
921
|
+
static toTerminalEnvironment(environment) {
|
|
922
|
+
const terminalEnvironment = {};
|
|
923
|
+
for (const [name, value] of Object.entries(environment)) {
|
|
924
|
+
if (value !== undefined && !Resources.hostVariables.includes(name.toUpperCase()))
|
|
925
|
+
terminalEnvironment[name] = value;
|
|
926
|
+
}
|
|
927
|
+
terminalEnvironment[Resources.termVariable] = Resources.terminalName;
|
|
928
|
+
terminalEnvironment[Resources.colorTermVariable] = Resources.colorTerm;
|
|
929
|
+
return terminalEnvironment;
|
|
930
|
+
}
|
|
931
|
+
}
|