dsh-bash-terminal-ts 0.8.7 → 0.9.0
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/CHANGELOG.de.md +152 -0
- package/CHANGELOG.es.md +152 -0
- package/CHANGELOG.fr.md +152 -0
- package/CHANGELOG.it.md +152 -0
- package/CHANGELOG.md +201 -0
- package/CHANGELOG.ru.md +152 -0
- package/README.de.md +1 -1
- package/README.en.md +3 -2
- package/README.es.md +1 -1
- package/README.it.md +1 -1
- package/README.md +4 -3
- package/agent-presets/README.md +171 -170
- package/agent-presets/install-presets.mjs +180 -179
- package/agent-presets/standard-win-niu/agent.cordis.yml +271 -0
- package/agent-presets/standard-win-niu/preset.yml +2 -0
- package/cordis.patch.yml +84 -83
- package/dist/client.core.js +10 -1
- package/dist/client.js +10 -1
- package/docs/dsh-plugin-development.md +169 -0
- package/docs/niu-backend.md +197 -0
- package/dsh.plugin.json +22 -22
- package/lib/client.js +10 -1
- package/lib/dsh-types.d.ts +14 -2
- package/lib/index.d.ts +15 -2
- package/lib/index.js +43 -5
- package/lib/locales.js +9 -0
- package/lib/shell-id.d.ts +1 -1
- package/lib/shells.d.ts +1 -0
- package/lib/shells.js +8 -2
- package/lib/terminal.js +4 -0
- package/lib/webterminals/host.js +2 -2
- package/lib/webterminals/mount.js +1 -0
- package/locale/de.json +6 -4
- package/locale/en.json +6 -4
- package/locale/es.json +6 -4
- package/locale/fr.json +6 -4
- package/locale/it.json +6 -4
- package/locale/ja.json +6 -4
- package/locale/ko.json +6 -4
- package/locale/ru.json +6 -4
- package/locale/zh.json +6 -4
- package/package.json +25 -11
- package/scripts/check-artifacts.mjs +96 -93
- package/src/client.tsx +685 -685
- package/src/dsh-types.ts +332 -320
- package/src/index.ts +1316 -1275
- package/src/locales.ts +461 -452
- package/src/shell-id.ts +11 -11
- package/src/shells.ts +279 -272
- package/src/terminal.ts +616 -612
- package/src/webterminals/host.ts +361 -361
- package/src/webterminals/mount.ts +237 -236
package/src/shell-id.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// The shell identifier, in its own module on purpose.
|
|
2
|
-
//
|
|
3
|
-
// `shells.ts` needs `NodeJS.Platform` and the resolved-path types, so it belongs
|
|
4
|
-
// to the host compile graph only. The browser half still has to name the same
|
|
5
|
-
// four ids (in the socket frames and the catalog rows it renders), so the id type
|
|
6
|
-
// lives here - a module with no Node references at all, which is what lets it sit
|
|
7
|
-
// in both tsconfig graphs without dragging `@types/node` into the client purity
|
|
8
|
-
// gate.
|
|
9
|
-
|
|
10
|
-
/** The five backends, in catalog order (`payload` = the pinned POSIX runtime). */
|
|
11
|
-
export type ShellId = "powershell" | "gitbash" | "msys2" | "wsl" | "payload";
|
|
1
|
+
// The shell identifier, in its own module on purpose.
|
|
2
|
+
//
|
|
3
|
+
// `shells.ts` needs `NodeJS.Platform` and the resolved-path types, so it belongs
|
|
4
|
+
// to the host compile graph only. The browser half still has to name the same
|
|
5
|
+
// four ids (in the socket frames and the catalog rows it renders), so the id type
|
|
6
|
+
// lives here - a module with no Node references at all, which is what lets it sit
|
|
7
|
+
// in both tsconfig graphs without dragging `@types/node` into the client purity
|
|
8
|
+
// gate.
|
|
9
|
+
|
|
10
|
+
/** The five backends, in catalog order (`payload` = the pinned POSIX runtime). */
|
|
11
|
+
export type ShellId = "powershell" | "gitbash" | "msys2" | "wsl" | "payload" | "niu";
|
package/src/shells.ts
CHANGED
|
@@ -1,272 +1,279 @@
|
|
|
1
|
-
// dsh-bash-terminal-ts: the shell catalog for the browser-side terminal.
|
|
2
|
-
//
|
|
3
|
-
// Why this file exists separately from `buildArgv` / `terminalArgv` / `buildEnv`
|
|
4
|
-
// in index.ts and terminal.ts: those three serve the MODEL tool, where a dumb
|
|
5
|
-
// terminal and a stripped environment are correct (no colour, no pager, one
|
|
6
|
-
// command per call). The browser terminal is the opposite contract - a real
|
|
7
|
-
// xterm emulator driven by a human who expects their profile, their prompt and
|
|
8
|
-
// their colours. Sharing the tool's argv/env would hand the user a shell with
|
|
9
|
-
// `TERM=dumb`, `NO_COLOR=1` and `-NoProfile`.
|
|
10
|
-
//
|
|
11
|
-
// So this module owns the UI-facing rules and REUSES the parts that are
|
|
12
|
-
// genuinely shared: the executable candidate lists (injected by index.ts, which
|
|
13
|
-
// keeps the dependency one-way and this file unit-testable), the MSYS2 `MSYSTEM`
|
|
14
|
-
// rule, and the WSL interactive argv shape.
|
|
15
|
-
//
|
|
16
|
-
// Everything here is pure: no fs, no child_process, no module-level cache. The
|
|
17
|
-
// caller injects `fsProbe` / `distros` and owns the TTL.
|
|
18
|
-
|
|
19
|
-
import type { ResolvedPaths } from "./dsh-types.js";
|
|
20
|
-
import type { ShellId } from "./shell-id.js";
|
|
21
|
-
|
|
22
|
-
/** Re-exported so existing importers keep working (see shell-id.ts for why). */
|
|
23
|
-
export type { ShellId };
|
|
24
|
-
|
|
25
|
-
/** Catalog order = fallback order: powershell is BOTH first choice and floor. The
|
|
26
|
-
* pinned POSIX payload sits last: it only earns a seat when the machine's own
|
|
27
|
-
* shells are absent, and a detected WSL distro vetoes it before that. */
|
|
28
|
-
export const SHELL_PRIORITY: readonly ShellId[] = ["powershell", "gitbash", "msys2", "wsl", "payload"];
|
|
29
|
-
|
|
30
|
-
/** Why one backend is not launchable (drives the settings-row badge). */
|
|
31
|
-
export type ShellUnavailable = "installed" | "not-installed" | "no-distro" | "launcher-only" | "wsl-preferred";
|
|
32
|
-
|
|
33
|
-
/** One catalog row: what the UI shows and what the host would spawn. */
|
|
34
|
-
export interface ShellEntry {
|
|
35
|
-
readonly id: ShellId;
|
|
36
|
-
/** Display label (a catalog constant, never inferred from the file name). */
|
|
37
|
-
readonly label: string;
|
|
38
|
-
/** Resolved executable, absent when the backend is not usable. */
|
|
39
|
-
readonly path?: string;
|
|
40
|
-
readonly available: boolean;
|
|
41
|
-
readonly reason: ShellUnavailable;
|
|
42
|
-
/** The activation switch state (powershell is always true: the floor). */
|
|
43
|
-
readonly activated: boolean;
|
|
44
|
-
/** False only for powershell, which the user cannot turn off. */
|
|
45
|
-
readonly togglable: boolean;
|
|
46
|
-
/** Interactive argv, present only when `available`. */
|
|
47
|
-
readonly argv?: readonly string[];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** The activation switches as persisted in the plugin config. */
|
|
51
|
-
export interface ShellSwitches {
|
|
52
|
-
gitbash: boolean;
|
|
53
|
-
msys2: boolean;
|
|
54
|
-
wsl: boolean;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
case "
|
|
124
|
-
return [requirePath(paths.
|
|
125
|
-
case "
|
|
126
|
-
return [requirePath(paths.
|
|
127
|
-
case "
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
readonly
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
1
|
+
// dsh-bash-terminal-ts: the shell catalog for the browser-side terminal.
|
|
2
|
+
//
|
|
3
|
+
// Why this file exists separately from `buildArgv` / `terminalArgv` / `buildEnv`
|
|
4
|
+
// in index.ts and terminal.ts: those three serve the MODEL tool, where a dumb
|
|
5
|
+
// terminal and a stripped environment are correct (no colour, no pager, one
|
|
6
|
+
// command per call). The browser terminal is the opposite contract - a real
|
|
7
|
+
// xterm emulator driven by a human who expects their profile, their prompt and
|
|
8
|
+
// their colours. Sharing the tool's argv/env would hand the user a shell with
|
|
9
|
+
// `TERM=dumb`, `NO_COLOR=1` and `-NoProfile`.
|
|
10
|
+
//
|
|
11
|
+
// So this module owns the UI-facing rules and REUSES the parts that are
|
|
12
|
+
// genuinely shared: the executable candidate lists (injected by index.ts, which
|
|
13
|
+
// keeps the dependency one-way and this file unit-testable), the MSYS2 `MSYSTEM`
|
|
14
|
+
// rule, and the WSL interactive argv shape.
|
|
15
|
+
//
|
|
16
|
+
// Everything here is pure: no fs, no child_process, no module-level cache. The
|
|
17
|
+
// caller injects `fsProbe` / `distros` and owns the TTL.
|
|
18
|
+
|
|
19
|
+
import type { ResolvedPaths } from "./dsh-types.js";
|
|
20
|
+
import type { ShellId } from "./shell-id.js";
|
|
21
|
+
|
|
22
|
+
/** Re-exported so existing importers keep working (see shell-id.ts for why). */
|
|
23
|
+
export type { ShellId };
|
|
24
|
+
|
|
25
|
+
/** Catalog order = fallback order: powershell is BOTH first choice and floor. The
|
|
26
|
+
* pinned POSIX payload sits last: it only earns a seat when the machine's own
|
|
27
|
+
* shells are absent, and a detected WSL distro vetoes it before that. */
|
|
28
|
+
export const SHELL_PRIORITY: readonly ShellId[] = ["powershell", "gitbash", "msys2", "wsl", "niu", "payload"];
|
|
29
|
+
|
|
30
|
+
/** Why one backend is not launchable (drives the settings-row badge). */
|
|
31
|
+
export type ShellUnavailable = "installed" | "not-installed" | "no-distro" | "launcher-only" | "wsl-preferred";
|
|
32
|
+
|
|
33
|
+
/** One catalog row: what the UI shows and what the host would spawn. */
|
|
34
|
+
export interface ShellEntry {
|
|
35
|
+
readonly id: ShellId;
|
|
36
|
+
/** Display label (a catalog constant, never inferred from the file name). */
|
|
37
|
+
readonly label: string;
|
|
38
|
+
/** Resolved executable, absent when the backend is not usable. */
|
|
39
|
+
readonly path?: string;
|
|
40
|
+
readonly available: boolean;
|
|
41
|
+
readonly reason: ShellUnavailable;
|
|
42
|
+
/** The activation switch state (powershell is always true: the floor). */
|
|
43
|
+
readonly activated: boolean;
|
|
44
|
+
/** False only for powershell, which the user cannot turn off. */
|
|
45
|
+
readonly togglable: boolean;
|
|
46
|
+
/** Interactive argv, present only when `available`. */
|
|
47
|
+
readonly argv?: readonly string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The activation switches as persisted in the plugin config. */
|
|
51
|
+
export interface ShellSwitches {
|
|
52
|
+
gitbash: boolean;
|
|
53
|
+
msys2: boolean;
|
|
54
|
+
wsl: boolean;
|
|
55
|
+
niu: boolean;
|
|
56
|
+
payload: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Everything the catalog builder needs from the outside world. */
|
|
60
|
+
export interface CatalogFacts {
|
|
61
|
+
/** Resolved executables (config override or candidate-list winner). */
|
|
62
|
+
paths: ResolvedPaths;
|
|
63
|
+
/** Whether one path is a launchable file. */
|
|
64
|
+
fsProbe(path: string): boolean;
|
|
65
|
+
/** `wsl -l -q` output. `undefined` means the probe could not answer (a timeout
|
|
66
|
+
* is not evidence of absence); `[]` means it answered "no distro installed". */
|
|
67
|
+
readonly distros: readonly string[] | undefined;
|
|
68
|
+
switches: ShellSwitches;
|
|
69
|
+
/** WSL distro override from config ('' = the WSL default). */
|
|
70
|
+
wslDistro: string;
|
|
71
|
+
/**
|
|
72
|
+
* True when the payload gate was vetoed by a positively-detected WSL distro
|
|
73
|
+
* (a real Linux bash outranks the MSYS payload). It only colors the payload
|
|
74
|
+
* row's badge when no payload path resolved - "why" information, not a gate.
|
|
75
|
+
*/
|
|
76
|
+
payloadSuppressed?: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Display labels. The catalog id/label pair is DECIDED BY THIS TABLE. */
|
|
80
|
+
const LABELS: Record<ShellId, string> = {
|
|
81
|
+
powershell: "PowerShell",
|
|
82
|
+
gitbash: "Git Bash",
|
|
83
|
+
msys2: "MSYS2",
|
|
84
|
+
wsl: "WSL bash",
|
|
85
|
+
niu: "Niubash",
|
|
86
|
+
payload: "POSIX payload",
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* `<SystemRoot>\System32\bash.exe` (and the sysnative mirror) is the deprecated
|
|
91
|
+
* WSL forwarder, not a shell. `candidateGitBashPaths` already skips it; this
|
|
92
|
+
* guard protects the second way a path reaches the catalog - a user-supplied
|
|
93
|
+
* `gitBashPath` / `msys2Path` config value.
|
|
94
|
+
*/
|
|
95
|
+
export function isWslLegacyBash(path: string): boolean {
|
|
96
|
+
const stem = /([^\\/]+)\.exe$/i.exec(path)?.[1]?.toLowerCase();
|
|
97
|
+
if (stem !== "bash") return false;
|
|
98
|
+
return /\\(windows\\system32|sysnative)\\bash\.exe$/i.test(path.replace(/\//g, "\\"));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Console-allocating launchers: `git-bash.exe`, `msys2.exe`, `msys2_shell.cmd`.
|
|
103
|
+
* They open their own window and return zero bytes on a piped stdio (measured
|
|
104
|
+
* against MSYS2 with bash 5.3.15), so they can never host an embedded terminal.
|
|
105
|
+
*/
|
|
106
|
+
export function isConsoleLauncher(path: string): boolean {
|
|
107
|
+
const name = path.slice(Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + 1).toLowerCase();
|
|
108
|
+
return name === "git-bash.exe" || name === "msys2.exe" || name === "msys2_shell.cmd";
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Interactive argv for the BROWSER terminal.
|
|
113
|
+
*
|
|
114
|
+
* Differences from the tool's `terminalArgv`: PowerShell keeps its profile (the
|
|
115
|
+
* user's aliases and prompt are the point of a UI terminal), and the three bash
|
|
116
|
+
* families are unchanged because those rules are about the shell itself:
|
|
117
|
+
* Git Bash needs `-i`, MSYS2 needs `-l` and NEVER `-lc` (this argv carries no
|
|
118
|
+
* command, and `bash -lc` with no operand exits 2), WSL needs `-- bash -i` on the
|
|
119
|
+
* default distro (bare `-e` fails under ConPTY with RPC 0x8007072c).
|
|
120
|
+
*/
|
|
121
|
+
export function uiArgv(shell: ShellId, paths: ResolvedPaths, wslDistro = ""): string[] {
|
|
122
|
+
switch (shell) {
|
|
123
|
+
case "powershell":
|
|
124
|
+
return [requirePath(paths.pwsh, shell), "-NoLogo"];
|
|
125
|
+
case "gitbash":
|
|
126
|
+
return [requirePath(paths.gitbash, shell), "-i"];
|
|
127
|
+
case "msys2":
|
|
128
|
+
return [requirePath(paths.msys2, shell), "-l"];
|
|
129
|
+
case "payload":
|
|
130
|
+
// The payload is a Git-for-Windows tree: the interactive rules are the
|
|
131
|
+
// gitbash ones (an interactive non-login shell; its /etc still works).
|
|
132
|
+
return [requirePath(paths.payload, shell), "-i"];
|
|
133
|
+
case "niu":
|
|
134
|
+
// Interactive non-login: niu reads ~/.niubashrc (its only Windows
|
|
135
|
+
// startup file), so the user's prompt and colours survive.
|
|
136
|
+
return [requirePath(paths.niu, shell), "-i"];
|
|
137
|
+
case "wsl": {
|
|
138
|
+
const distro = wslDistro.trim();
|
|
139
|
+
return distro.length > 0
|
|
140
|
+
? [requirePath(paths.wsl, shell), "-d", distro, "-e", "bash", "-i"]
|
|
141
|
+
: [requirePath(paths.wsl, shell), "--", "bash", "-i"];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Environment for the BROWSER terminal: the host process environment plus a real
|
|
148
|
+
* terminal type. `buildEnv` is deliberately NOT reused - it layers
|
|
149
|
+
* `TERM=dumb`/`NO_COLOR=1`/`PAGER=cat` over an agent-scoped map, which is right
|
|
150
|
+
* for a one-shot tool call and wrong for an interactive screen.
|
|
151
|
+
*
|
|
152
|
+
* The one rule carried over is MSYS2's `MSYSTEM`: without it MSYS2 starts in the
|
|
153
|
+
* bare MSYS environment and `/etc/profile` never puts `/mingw64/bin` on PATH, so
|
|
154
|
+
* gcc/make are missing in the very shell the user chose to get them from. An
|
|
155
|
+
* explicit value in the base environment wins, exactly as in `buildEnv`.
|
|
156
|
+
*/
|
|
157
|
+
export function uiEnv(shell: ShellId, baseEnv: NodeJS.ProcessEnv): Record<string, string | undefined> {
|
|
158
|
+
const env: Record<string, string | undefined> = { ...baseEnv };
|
|
159
|
+
// A UI terminal is an xterm: the tool's `dumb` would kill colours, cursor
|
|
160
|
+
// control and full-screen programs.
|
|
161
|
+
env.TERM = "xterm-256color";
|
|
162
|
+
if (shell === "msys2" && env.MSYSTEM === undefined) env.MSYSTEM = "MINGW64";
|
|
163
|
+
return env;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function requirePath(path: string | undefined, shell: ShellId): string {
|
|
167
|
+
if (path === undefined || path.length === 0) {
|
|
168
|
+
throw new Error(`terminal: ${shell} backend unavailable - no executable resolved`);
|
|
169
|
+
}
|
|
170
|
+
return path;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** One row's availability, given the injected facts. */
|
|
174
|
+
function probe(id: ShellId, facts: CatalogFacts): { available: boolean; reason: ShellUnavailable; path?: string } {
|
|
175
|
+
const path =
|
|
176
|
+
id === "powershell" ? facts.paths.pwsh
|
|
177
|
+
: id === "gitbash" ? facts.paths.gitbash
|
|
178
|
+
: id === "msys2" ? facts.paths.msys2
|
|
179
|
+
: id === "payload" ? facts.paths.payload
|
|
180
|
+
: id === "niu" ? facts.paths.niu
|
|
181
|
+
: facts.paths.wsl;
|
|
182
|
+
if (path === undefined || path.length === 0) {
|
|
183
|
+
if (id === "payload" && facts.payloadSuppressed === true) return { available: false, reason: "wsl-preferred" };
|
|
184
|
+
return { available: false, reason: "not-installed" };
|
|
185
|
+
}
|
|
186
|
+
// A configured path is honoured, but a launcher is not a shell: trusting the
|
|
187
|
+
// config here is how `msys2.exe` ends up in the menu and returns no output.
|
|
188
|
+
if (isConsoleLauncher(path)) return { available: false, reason: "launcher-only", path };
|
|
189
|
+
if (isWslLegacyBash(path)) return { available: false, reason: "launcher-only", path };
|
|
190
|
+
if (!facts.fsProbe(path)) return { available: false, reason: "not-installed", path };
|
|
191
|
+
if (id === "wsl" && facts.distros !== undefined && facts.distros.length === 0) return { available: false, reason: "no-distro", path };
|
|
192
|
+
return { available: true, reason: "installed", path };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The catalog the settings page and the spawn path both read.
|
|
197
|
+
*
|
|
198
|
+
* `powershell` is never `activated: false` even if a switch said so: it is the
|
|
199
|
+
* floor of the fallback chain (R2/R6), and a floor that can be removed is not a
|
|
200
|
+
* floor. The caller sees that through `togglable: false`.
|
|
201
|
+
*/
|
|
202
|
+
export function buildCatalog(facts: CatalogFacts): ShellEntry[] {
|
|
203
|
+
return SHELL_PRIORITY.map((id) => {
|
|
204
|
+
const probed = probe(id, facts);
|
|
205
|
+
const activated = id === "powershell" ? true : probed.available && facts.switches[id] === true;
|
|
206
|
+
const argv = activated && probed.available ? uiArgv(id, facts.paths, facts.wslDistro) : undefined;
|
|
207
|
+
return {
|
|
208
|
+
id,
|
|
209
|
+
label: LABELS[id],
|
|
210
|
+
...(probed.path !== undefined ? { path: probed.path } : {}),
|
|
211
|
+
available: probed.available,
|
|
212
|
+
reason: probed.reason,
|
|
213
|
+
activated,
|
|
214
|
+
// Only powershell is un-togglable, and that is the floor rule above.
|
|
215
|
+
togglable: id !== "powershell",
|
|
216
|
+
...(argv !== undefined ? { argv } : {}),
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** The result of resolving which shell a terminal should start with. */
|
|
222
|
+
export interface ResolvedDefault {
|
|
223
|
+
readonly id: ShellId | "cmd";
|
|
224
|
+
readonly path: string;
|
|
225
|
+
readonly argv: readonly string[];
|
|
226
|
+
/** True when the stored value was replaced (the UI shows why). */
|
|
227
|
+
readonly fellBack: boolean;
|
|
228
|
+
/** True when nothing is activated and the floor was used. */
|
|
229
|
+
readonly floor: boolean;
|
|
230
|
+
/** The stored value that was not honoured, when there was one. */
|
|
231
|
+
readonly from?: string;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Decide the shell for one allocation (R4/R5/R6).
|
|
236
|
+
*
|
|
237
|
+
* Three distinct outcomes, because the UI has to say different things about them:
|
|
238
|
+
* - `""` means the user never chose: take the first candidate, no notice;
|
|
239
|
+
* - a non-empty value outside the candidates means the choice went stale (a
|
|
240
|
+
* switch was turned off, or a shell was uninstalled): fall back AND report
|
|
241
|
+
* `fellBack` with the original value;
|
|
242
|
+
* - no candidate at all: the floor is PowerShell's own chain (pwsh 7 -> Windows
|
|
243
|
+
* PowerShell), and `cmd.exe` is the last resort when even that is gone.
|
|
244
|
+
*
|
|
245
|
+
* The stored config is never rewritten here - a fallback that persists would
|
|
246
|
+
* silently eat the user's choice.
|
|
247
|
+
*/
|
|
248
|
+
export function resolveDefaultShell(catalog: readonly ShellEntry[], configured: string, paths: ResolvedPaths): ResolvedDefault {
|
|
249
|
+
const usable = SHELL_PRIORITY
|
|
250
|
+
.map((id) => catalog.find((entry) => entry.id === id))
|
|
251
|
+
.filter((entry): entry is ShellEntry => entry !== undefined && entry.activated && entry.available && entry.path !== undefined);
|
|
252
|
+
const wanted = configured.trim();
|
|
253
|
+
const chosen = wanted.length === 0 ? undefined : usable.find((entry) => entry.id === wanted);
|
|
254
|
+
const picked = chosen ?? usable[0];
|
|
255
|
+
if (picked !== undefined) {
|
|
256
|
+
return {
|
|
257
|
+
id: picked.id,
|
|
258
|
+
path: picked.path!,
|
|
259
|
+
argv: picked.argv ?? uiArgv(picked.id, paths),
|
|
260
|
+
fellBack: chosen === undefined && wanted.length > 0,
|
|
261
|
+
floor: false,
|
|
262
|
+
...(chosen === undefined && wanted.length > 0 ? { from: wanted } : {}),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
// Nothing activated: the floor. `paths.pwsh` is already the candidate-list
|
|
266
|
+
// winner (pwsh 7 -> PATH pwsh -> Windows PowerShell 5.1), so the only step
|
|
267
|
+
// below it is cmd.exe, which exists on every Windows host and is enough to
|
|
268
|
+
// type in.
|
|
269
|
+
const floorPath = paths.pwsh ?? "C:\\Windows\\System32\\cmd.exe";
|
|
270
|
+
const isCmd = /\cmd\.exe$/i.test(floorPath);
|
|
271
|
+
return {
|
|
272
|
+
id: isCmd ? "cmd" : "powershell",
|
|
273
|
+
path: floorPath,
|
|
274
|
+
argv: isCmd ? [floorPath] : [floorPath, "-NoLogo"],
|
|
275
|
+
fellBack: wanted.length > 0,
|
|
276
|
+
floor: true,
|
|
277
|
+
...(wanted.length > 0 ? { from: wanted } : {}),
|
|
278
|
+
};
|
|
279
|
+
}
|