Repository navigation
Expand file tree
/
Copy pathexternalLauncher.ts
More file actions
807 lines (737 loc) · 27.1 KB
/
Copy pathexternalLauncher.ts
File metadata and controls
807 lines (737 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
124
125
126
127
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
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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/**
* ExternalLauncher - external application launch service interface.
*
* Owns process launch helpers for browser URLs and workspace paths
* in configured editor integrations.
*
* @module ExternalLauncher
*/
import {
EDITORS,
ExternalLauncherError,
ExternalLauncherBrowserSpawnError,
ExternalLauncherCommandNotFoundError,
ExternalLauncherEditorSpawnError,
ExternalLauncherUnknownEditorError,
ExternalLauncherUnsupportedEditorError,
type EditorId,
type FileManagerRevealKind,
type LaunchEditorInput,
} from "@t3tools/contracts";
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell";
import * as Clock from "effect/Clock";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Encoding from "effect/Encoding";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Ref from "effect/Ref";
import * as Stream from "effect/Stream";
import * as ChildProcess from "effect/unstable/process/ChildProcess";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
// ==============================
// Definitions
// ==============================
export {
ExternalLauncherError,
ExternalLauncherBrowserSpawnError,
ExternalLauncherCommandNotFoundError,
ExternalLauncherEditorSpawnError,
ExternalLauncherUnknownEditorError,
ExternalLauncherUnsupportedEditorError,
} from "@t3tools/contracts";
export type { LaunchEditorInput };
interface EditorLaunch {
readonly editor: EditorId;
readonly target: string;
readonly command: string;
readonly args: ReadonlyArray<string>;
}
interface ProcessLaunch {
readonly command: string;
readonly args: ReadonlyArray<string>;
readonly options: ChildProcess.CommandOptions;
}
interface TargetPathAndPosition {
readonly path: string;
readonly line: string;
readonly column: Option.Option<string>;
}
const TARGET_WITH_POSITION_PATTERN = /^(.*?):(\d+)(?::(\d+))?$/;
const POWERSHELL_ARGUMENTS_PREFIX = [
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-EncodedCommand",
] as const;
const DETACHED_IGNORE_STDIO_OPTIONS = {
detached: true,
stdin: "ignore",
stdout: "ignore",
stderr: "ignore",
} as const satisfies ChildProcess.CommandOptions;
const compactEnv = (input: Record<string, Option.Option<string>>): NodeJS.ProcessEnv =>
Object.fromEntries(
Object.entries(input).flatMap(([key, value]) =>
Option.match(value, {
onNone: () => [],
onSome: (resolved) => [[key, resolved]],
}),
),
);
const BrowserLaunchEnvConfig = Config.all({
SYSTEMROOT: Config.string("SYSTEMROOT").pipe(Config.option),
windir: Config.string("windir").pipe(Config.option),
WSL_DISTRO_NAME: Config.string("WSL_DISTRO_NAME").pipe(Config.option),
WSL_INTEROP: Config.string("WSL_INTEROP").pipe(Config.option),
SSH_CONNECTION: Config.string("SSH_CONNECTION").pipe(Config.option),
SSH_TTY: Config.string("SSH_TTY").pipe(Config.option),
container: Config.string("container").pipe(Config.option),
DISPLAY: Config.string("DISPLAY").pipe(Config.option),
WAYLAND_DISPLAY: Config.string("WAYLAND_DISPLAY").pipe(Config.option),
}).pipe(Config.map(compactEnv));
const CommandLookupEnvConfig = Config.all({
PATH: Config.string("PATH").pipe(Config.option),
Path: Config.string("Path").pipe(Config.option),
path: Config.string("path").pipe(Config.option),
PATHEXT: Config.string("PATHEXT").pipe(Config.option),
}).pipe(Config.map(compactEnv));
const readBrowserLaunchEnv = BrowserLaunchEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
const readCommandLookupEnv = CommandLookupEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
function parseTargetPathAndPosition(target: string): Option.Option<TargetPathAndPosition> {
const match = TARGET_WITH_POSITION_PATTERN.exec(target);
if (!match?.[1] || !match[2]) {
return Option.none();
}
return Option.some({
path: match[1],
line: match[2],
column: Option.fromUndefinedOr(match[3]),
});
}
function resolveCommandEditorArgs(
editor: (typeof EDITORS)[number],
target: string,
): ReadonlyArray<string> {
const parsedTarget = parseTargetPathAndPosition(target);
switch (editor.launchStyle) {
case "direct-path":
return [target];
case "goto":
return Option.isSome(parsedTarget) ? ["--goto", target] : [target];
case "line-column":
return Option.match(parsedTarget, {
onNone: () => [target],
onSome: ({ path, line, column }) => [
"--line",
line,
...Option.match(column, {
onNone: () => [],
onSome: (value) => ["--column", value],
}),
path,
],
});
}
}
function resolveEditorArgs(
editor: (typeof EDITORS)[number],
target: string,
): ReadonlyArray<string> {
const baseArgs = "baseArgs" in editor ? editor.baseArgs : [];
return [...baseArgs, ...resolveCommandEditorArgs(editor, target)];
}
const resolveAvailableCommand = Effect.fn("externalLauncher.resolveAvailableCommand")(function* (
commands: ReadonlyArray<string>,
env: NodeJS.ProcessEnv,
): Effect.fn.Return<Option.Option<string>, never, FileSystem.FileSystem | Path.Path> {
for (const command of commands) {
if (yield* isCommandAvailable(command, { env })) {
return Option.some(command);
}
}
return Option.none();
});
function encodeUtf16LeBase64(input: string): string {
const bytes = new Uint8Array(input.length * 2);
for (let index = 0; index < input.length; index += 1) {
const code = input.charCodeAt(index);
bytes[index * 2] = code & 0xff;
bytes[index * 2 + 1] = code >>> 8;
}
return Encoding.encodeBase64(bytes);
}
function escapePowerShellStringLiteral(input: string): string {
return `'${input.replaceAll("'", "''")}'`;
}
function resolvePowerShellPath(env: NodeJS.ProcessEnv = {}): string {
return `${env.SYSTEMROOT || env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
}
function resolveWslPowerShellPath(): string {
return "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe";
}
// File reveals from WSL resolve PowerShell through the interop PATH rather
// than the fixed /mnt/c mount: the automount root is configurable, and a
// PATH-resolved command keeps the advertised capability aligned with the
// availability check `launchEditor` performs before spawning.
const WSL_POWERSHELL_COMMAND = "powershell.exe";
function shouldUseWindowsHostFromWsl(
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv = {},
): boolean {
return (
platform === "linux" &&
(env.WSL_DISTRO_NAME !== undefined || env.WSL_INTEROP !== undefined) &&
env.SSH_CONNECTION === undefined &&
env.SSH_TTY === undefined &&
env.container === undefined
);
}
function resolveWindowsBrowserLaunch(target: string, command: string): ProcessLaunch {
const encodedCommand = encodeUtf16LeBase64(
`$ProgressPreference = 'SilentlyContinue'; Start ${escapePowerShellStringLiteral(target)}`,
);
return {
command,
args: [...POWERSHELL_ARGUMENTS_PREFIX, encodedCommand],
options: {
detached: true,
shell: false,
stdin: "ignore",
stdout: "ignore",
stderr: "ignore",
},
};
}
function hasGraphicalLinuxSession(env: NodeJS.ProcessEnv): boolean {
return [env.DISPLAY, env.WAYLAND_DISPLAY].some(
(value) => value !== undefined && value.trim().length > 0,
);
}
function fileManagerCommandForPlatform(
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv,
): string | undefined {
switch (platform) {
case "darwin":
return "open";
case "win32":
return "explorer";
default:
if (shouldUseWindowsHostFromWsl(platform, env)) {
return env.WSL_DISTRO_NAME?.trim() ? "explorer.exe" : undefined;
}
return hasGraphicalLinuxSession(env) ? "xdg-open" : undefined;
}
}
// A graphical session variable plus an executable `xdg-open` does not prove
// that opening a directory does anything: without an `inode/directory` MIME
// handler, `xdg-open` exits nonzero after the launcher has already detached,
// so the client would see a silent no-op. Require the handler before
// advertising the file manager on Linux.
//
// The probe carries its own timeout well inside the scan timeout
// `server.getConfig` applies to editor discovery: that outer timeout degrades
// to an empty editor list, so a hung `xdg-mime` (broken D-Bus or desktop
// session) must cost only the file manager, not every discovered editor.
const LINUX_DIRECTORY_HANDLER_PROBE_TIMEOUT = "2 seconds";
const hasUsableLinuxDirectoryHandler = Effect.fn("externalLauncher.hasUsableLinuxDirectoryHandler")(
function* (
env: NodeJS.ProcessEnv,
): Effect.fn.Return<
boolean,
never,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
if (!(yield* isCommandAvailable("xdg-mime", { env }))) {
return false;
}
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
return yield* spawner
.spawn(
ChildProcess.make("xdg-mime", ["query", "default", "inode/directory"], {
stdin: "ignore",
stderr: "ignore",
}),
)
.pipe(
Effect.flatMap((handle) =>
Effect.all([handle.stdout.pipe(Stream.decodeText(), Stream.mkString), handle.exitCode], {
concurrency: "unbounded",
}),
),
Effect.map(([stdout, exitCode]) => exitCode === 0 && stdout.trim().length > 0),
Effect.scoped,
Effect.timeout(LINUX_DIRECTORY_HANDLER_PROBE_TIMEOUT),
Effect.orElseSucceed(() => false),
);
},
);
const isUsableFileManagerCommand = Effect.fn("externalLauncher.isUsableFileManagerCommand")(
function* (
command: string,
env: NodeJS.ProcessEnv,
): Effect.fn.Return<
boolean,
never,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
if (!(yield* isCommandAvailable(command, { env }))) {
return false;
}
return command !== "xdg-open" || (yield* hasUsableLinuxDirectoryHandler(env));
},
);
// The file-manager command a launch can actually run, not just the platform
// preference. WSL hosts prefer the Windows Explorer bridge, but interop can
// exist without `explorer.exe` on PATH (appendWindowsPath=false) or without a
// distro name while WSLg still provides a working Linux file manager, so they
// keep the `xdg-open` fallback instead of losing the editor entirely.
const resolveUsableFileManagerCommand = Effect.fn(
"externalLauncher.resolveUsableFileManagerCommand",
)(function* (
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv,
): Effect.fn.Return<
string | undefined,
never,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
const command = fileManagerCommandForPlatform(platform, env);
if (command !== undefined && (yield* isUsableFileManagerCommand(command, env))) {
return command;
}
if (
shouldUseWindowsHostFromWsl(platform, env) &&
hasGraphicalLinuxSession(env) &&
(yield* isUsableFileManagerCommand("xdg-open", env))
) {
return "xdg-open";
}
return undefined;
});
// Reveal on Windows and WSL runs through PowerShell (see
// resolveFileManagerRevealLaunch), not the `explorer` command that gates the
// file-manager editor itself, so the capability must probe the executables the
// reveal actually spawns. Callers gate on file-manager availability first;
// the Linux "files" kind relies on that gate for the directory-handler probe,
// while the WSL fallback re-probes because its availability may have come
// from the Explorer bridge instead.
const fileManagerRevealKindForPlatform = Effect.fn(
"externalLauncher.fileManagerRevealKindForPlatform",
)(function* (
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv,
): Effect.fn.Return<
FileManagerRevealKind | undefined,
never,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
if (platform === "darwin") return "finder";
if (platform === "win32") {
return (yield* isCommandAvailable(resolvePowerShellPath(env), { env }))
? "file-explorer"
: undefined;
}
if (shouldUseWindowsHostFromWsl(platform, env)) {
if (
env.WSL_DISTRO_NAME?.trim() &&
(yield* isCommandAvailable("explorer.exe", { env })) &&
(yield* isCommandAvailable(WSL_POWERSHELL_COMMAND, { env }))
) {
return "file-explorer";
}
return hasGraphicalLinuxSession(env) && (yield* isUsableFileManagerCommand("xdg-open", env))
? "files"
: undefined;
}
return hasGraphicalLinuxSession(env) ? "files" : undefined;
});
function resolveWslFileManagerPath(target: string, distroName: string): string {
const relativePath = target.replace(/^\/+/, "").replaceAll("/", "\\");
return `\\\\wsl.localhost\\${distroName}${relativePath.length > 0 ? `\\${relativePath}` : ""}`;
}
function buildBrowserLaunch(
target: string,
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv = {},
): ProcessLaunch {
if (platform === "darwin") {
return {
command: "open",
args: [target],
options: DETACHED_IGNORE_STDIO_OPTIONS,
};
}
if (platform === "win32") {
return resolveWindowsBrowserLaunch(target, resolvePowerShellPath(env));
}
if (shouldUseWindowsHostFromWsl(platform, env)) {
return resolveWindowsBrowserLaunch(target, resolveWslPowerShellPath());
}
return {
command: "xdg-open",
args: [target],
options: DETACHED_IGNORE_STDIO_OPTIONS,
};
}
const buildAvailableEditors = Effect.fn("externalLauncher.buildAvailableEditors")(function* (
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv,
): Effect.fn.Return<
ReadonlyArray<EditorId>,
never,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
const available: EditorId[] = [];
for (const editor of EDITORS) {
if (editor.commands === null) {
if ((yield* resolveUsableFileManagerCommand(platform, env)) !== undefined) {
available.push(editor.id);
}
continue;
}
const command = yield* resolveAvailableCommand(editor.commands, env);
if (Option.isSome(command)) {
available.push(editor.id);
}
}
return available;
});
const resolveBrowserLaunch = Effect.fn("externalLauncher.resolveBrowserLaunch")(function* (
target: string,
) {
const platform = yield* HostProcessPlatform;
const env = yield* readBrowserLaunchEnv;
return buildBrowserLaunch(target, platform, env);
});
const resolveAvailableEditors = Effect.fn("externalLauncher.resolveAvailableEditors")(function* () {
const platform = yield* HostProcessPlatform;
const env = { ...(yield* readBrowserLaunchEnv), ...(yield* readCommandLookupEnv) };
return yield* buildAvailableEditors(platform, env);
});
const resolveFileManagerRevealKind = Effect.fn("externalLauncher.resolveFileManagerRevealKind")(
function* () {
const platform = yield* HostProcessPlatform;
const env = { ...(yield* readBrowserLaunchEnv), ...(yield* readCommandLookupEnv) };
return yield* fileManagerRevealKindForPlatform(platform, env);
},
);
// Editor discovery walks PATH for every known editor and runs for every
// client connect (the server config embeds the available editors). Memoize
// the discovered set for a bounded window so repeat connects skip even the
// per-command cache lookups in @t3tools/shared/shell.
//
// This deliberately does not use `Effect.cachedWithTTL`: that memoizes the
// first caller's Exit whatever it is, including an interrupt. Callers run this
// on the connection fiber under a timeout (`resolveAvailableEditorsForConfig`),
// so one client disconnecting mid-scan would cache the interrupt and replay it
// to every later connect for the whole TTL, breaking `server.getConfig`
// permanently. Storing only on success means an interrupted scan leaves the
// cache untouched and the next connect simply rescans.
// Expiry uses the monotonic clock (Clock.currentTimeNanos), matching the
// command-resolution cache in @t3tools/shared/shell, so a backward wall-clock
// adjustment cannot keep an expired entry alive.
const EDITOR_DISCOVERY_CACHE_TTL_NANOS = 60_000_000_000n;
interface EditorDiscoveryCacheEntry {
readonly editors: ReadonlyArray<EditorId>;
readonly expiresAtNanos: bigint;
}
/**
* ExternalLauncher - Service tag for browser/editor launch operations.
*/
export class ExternalLauncher extends Context.Service<
ExternalLauncher,
{
readonly resolveAvailableEditors: () => Effect.Effect<ReadonlyArray<EditorId>>;
/**
* Reveal kind for the host, or undefined when the executable a reveal
* actually spawns is unavailable. Only meaningful when
* `resolveAvailableEditors` includes "file-manager": on Linux that
* availability check also carries the directory-handler probe this
* capability relies on.
*/
readonly resolveFileManagerRevealKind: () => Effect.Effect<FileManagerRevealKind | undefined>;
/** Launch a URL target in the default browser. */
readonly launchBrowser: (target: string) => Effect.Effect<void, ExternalLauncherError>;
/**
* Launch a workspace path in a selected editor integration.
*
* Launches the editor as a detached process so server startup is not blocked.
*/
readonly launchEditor: (input: LaunchEditorInput) => Effect.Effect<void, ExternalLauncherError>;
}
>()("t3/process/externalLauncher") {}
// ==============================
// Implementations
// ==============================
const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
input: LaunchEditorInput,
): Effect.fn.Return<
EditorLaunch,
ExternalLauncherError,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
const platform = yield* HostProcessPlatform;
const env = { ...(yield* readBrowserLaunchEnv), ...(yield* readCommandLookupEnv) };
yield* Effect.annotateCurrentSpan({
"externalLauncher.editor": input.editor,
"externalLauncher.cwd": input.cwd,
"externalLauncher.platform": platform,
});
const editorDef = EDITORS.find((editor) => editor.id === input.editor);
if (!editorDef) {
return yield* new ExternalLauncherUnknownEditorError({ editor: input.editor });
}
if (editorDef.commands) {
const command = Option.getOrElse(
yield* resolveAvailableCommand(editorDef.commands, env),
() => editorDef.commands[0],
);
return {
editor: editorDef.id,
target: input.cwd,
command,
args: resolveEditorArgs(editorDef, input.cwd),
};
}
if (editorDef.id !== "file-manager") {
return yield* new ExternalLauncherUnsupportedEditorError({ editor: input.editor });
}
const command = yield* resolveUsableFileManagerCommand(platform, env);
if (command === undefined) {
return yield* new ExternalLauncherUnsupportedEditorError({ editor: input.editor });
}
if (input.reveal === true) {
return yield* resolveFileManagerRevealLaunch(input.cwd, platform, env, command);
}
return {
editor: editorDef.id,
target: input.cwd,
command,
args:
command === "explorer.exe" && env.WSL_DISTRO_NAME !== undefined
? [resolveWslFileManagerPath(input.cwd, env.WSL_DISTRO_NAME)]
: [input.cwd],
};
});
/**
* PowerShell source that launches File Explorer with its raw selection
* switch. Explorer's contract is the single argument `/select,"<path>"` with
* only the path quoted; Node's default spawn quoting wraps the whole argument
* when the path has spaces and Explorer misparses it, silently opening a
* fallback folder. A single `-ArgumentList` string in Windows PowerShell 5.1
* reaches the child's command line verbatim, preserving the raw switch.
*
* Exported so the Windows smoke test can drive the identical source through a
* real PowerShell against a recording stub instead of Explorer.
*/
export function buildFileExplorerRevealPowerShellSource(
explorerCommand: string,
target: string,
): string {
return `$ProgressPreference = 'SilentlyContinue'; Start-Process ${escapePowerShellStringLiteral(explorerCommand)} -ArgumentList ('/select,"' + ${escapePowerShellStringLiteral(target)} + '"')`;
}
function fileExplorerRevealLaunch(
target: string,
explorerTarget: string,
powershellCommand: string,
): EditorLaunch {
return {
editor: "file-manager",
target,
command: powershellCommand,
args: [
...POWERSHELL_ARGUMENTS_PREFIX,
encodeUtf16LeBase64(buildFileExplorerRevealPowerShellSource("explorer.exe", explorerTarget)),
],
};
}
function normalizeWindowsFileManagerPath(target: string): string {
return target.replaceAll("/", "\\");
}
const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch")(function* (
target: string,
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv,
// The command resolveUsableFileManagerCommand picked; a WSL host that fell
// back to the Linux file manager must reveal through it as well.
command: string,
): Effect.fn.Return<
EditorLaunch,
never,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
if (platform === "darwin") {
return { editor: "file-manager", target, command: "open", args: ["-R", target] };
}
if (platform === "win32") {
return fileExplorerRevealLaunch(
target,
normalizeWindowsFileManagerPath(target),
resolvePowerShellPath(env),
);
}
if (
command === "explorer.exe" &&
shouldUseWindowsHostFromWsl(platform, env) &&
env.WSL_DISTRO_NAME !== undefined
) {
const explorerTarget = resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME);
if (yield* isCommandAvailable(WSL_POWERSHELL_COMMAND, { env })) {
// Explorer's raw switch cannot express a double quote, and unlike
// Windows paths a WSL path may legally contain one: open the containing
// directory in File Explorer instead, matching the advertised
// "file-explorer" kind.
if (explorerTarget.includes('"')) {
const path = yield* Path.Path;
return {
editor: "file-manager",
target,
command: "explorer.exe",
args: [resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)],
};
}
return fileExplorerRevealLaunch(target, explorerTarget, WSL_POWERSHELL_COMMAND);
}
// Without interop PowerShell the capability advertised the Linux "files"
// kind when it advertised anything at all, so the reveal must open the
// Linux file manager the label promised, not File Explorer.
if (hasGraphicalLinuxSession(env) && (yield* isUsableFileManagerCommand("xdg-open", env))) {
const path = yield* Path.Path;
return { editor: "file-manager", target, command: "xdg-open", args: [path.dirname(target)] };
}
// Nothing was advertised here; open the parent in File Explorer as the
// best remaining effort for a stale client.
const path = yield* Path.Path;
return {
editor: "file-manager",
target,
command: "explorer.exe",
args: [resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)],
};
}
// Linux file managers have no portable "select this file" flag, so open
// the containing directory instead.
const path = yield* Path.Path;
return { editor: "file-manager", target, command, args: [path.dirname(target)] };
});
const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (
launch: ProcessLaunch,
onError: (cause: unknown) => ExternalLauncherError,
): Effect.fn.Return<void, ExternalLauncherError, ChildProcessSpawner.ChildProcessSpawner> {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const command = ChildProcess.make(launch.command, launch.args, launch.options);
yield* spawner.spawn(command).pipe(
Effect.flatMap((handle) => handle.unref),
Effect.asVoid,
Effect.scoped,
Effect.mapError(onError),
);
});
const launchBrowser = Effect.fn("externalLauncher.launchBrowser")(function* (
target: string,
): Effect.fn.Return<void, ExternalLauncherError, ChildProcessSpawner.ChildProcessSpawner> {
const launch = yield* resolveBrowserLaunch(target);
return yield* launchAndUnref(
launch,
(cause) =>
new ExternalLauncherBrowserSpawnError({
target,
command: launch.command,
args: launch.args,
cause,
}),
);
});
const launchEditorProcess = Effect.fn("externalLauncher.launchEditorProcess")(function* (
launch: EditorLaunch,
): Effect.fn.Return<
void,
ExternalLauncherError,
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
> {
const env = yield* readCommandLookupEnv;
if (!(yield* isCommandAvailable(launch.command, { env }))) {
return yield* new ExternalLauncherCommandNotFoundError({
editor: launch.editor,
command: launch.command,
});
}
const spawnCommand = yield* resolveSpawnCommand(launch.command, launch.args, { env });
yield* launchAndUnref(
{
command: spawnCommand.command,
args: spawnCommand.args,
options: {
detached: true,
shell: spawnCommand.shell,
stdin: "ignore",
stdout: "ignore",
stderr: "ignore",
},
},
(cause) =>
new ExternalLauncherEditorSpawnError({
editor: launch.editor,
target: launch.target,
command: spawnCommand.command,
args: spawnCommand.args,
cause,
}),
);
});
export const make = Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const provideCommandResolutionServices = <A, E, R>(
effect: Effect.Effect<A, E, R | FileSystem.FileSystem | Path.Path>,
) =>
effect.pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
);
const editorDiscoveryCache = yield* Ref.make<Option.Option<EditorDiscoveryCacheEntry>>(
Option.none(),
);
const cachedAvailableEditors = Effect.gen(function* () {
const nowNanos = yield* Clock.currentTimeNanos;
const entry = yield* Ref.get(editorDiscoveryCache);
if (Option.isSome(entry) && entry.value.expiresAtNanos > nowNanos) {
return entry.value.editors;
}
const editors = yield* provideCommandResolutionServices(resolveAvailableEditors()).pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
);
yield* Ref.set(
editorDiscoveryCache,
Option.some({
editors,
expiresAtNanos: nowNanos + EDITOR_DISCOVERY_CACHE_TTL_NANOS,
}),
);
return editors;
});
return ExternalLauncher.of({
resolveAvailableEditors: () => cachedAvailableEditors,
resolveFileManagerRevealKind: () =>
provideCommandResolutionServices(resolveFileManagerRevealKind()).pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
),
launchBrowser: (target) =>
launchBrowser(target).pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
),
launchEditor: (input) =>
provideCommandResolutionServices(
Effect.flatMap(resolveEditorLaunch(input), launchEditorProcess),
).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)),
});
});
export const layer = Layer.effect(ExternalLauncher, make);