Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as ConfigProvider from "effect/ConfigProvider";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
11
12import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
13import { SpawnExecutableResolution } from "@t3tools/shared/shell";
14import * as ExternalLauncher from "./externalLauncher.ts";
15
16function makeMockDetachedHandle(onUnref: () => void = () => undefined) {
17 return ChildProcessSpawner.makeHandle({
18 pid: ChildProcessSpawner.ProcessId(1),
19 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
20 isRunning: Effect.succeed(true),
21 kill: () => Effect.void,
22 unref: Effect.sync(() => {
23 onUnref();
24 return Effect.void;
25 }),
26 stdin: Sink.drain,
27 stdout: Stream.empty,
28 stderr: Stream.empty,
29 all: Stream.empty,
30 getInputFd: () => Sink.drain,
31 getOutputFd: () => Stream.empty,
32 });
33}
34
35const testLayer = (input: {
36 readonly platform: NodeJS.Platform;
37 readonly env?: Record<string, string>;
38 readonly resolveExecutable?: (command: string) => string | undefined;
39 readonly onSpawn?: (command: ChildProcess.StandardCommand) => void;
40 readonly onUnref?: () => void;
41}) => {
42 const spawnerLayer = Layer.succeed(
43 ChildProcessSpawner.ChildProcessSpawner,
44 ChildProcessSpawner.make((command) =>
45 Effect.sync(() => {
46 assert.equal(ChildProcess.isStandardCommand(command), true);
47 if (!ChildProcess.isStandardCommand(command)) {
48 throw new Error("Expected a standard command");
49 }
50 input.onSpawn?.(command);
51 return makeMockDetachedHandle(input.onUnref);
52 }),
53 ),
54 );
55
56 return Layer.mergeAll(
57 ExternalLauncher.layer.pipe(Layer.provide(Layer.merge(NodeServices.layer, spawnerLayer))),
58 Layer.succeed(HostProcessPlatform, input.platform),
59 Layer.succeed(
60 SpawnExecutableResolution,
61 (command) => input.resolveExecutable?.(command) ?? command,
62 ),
63 ConfigProvider.layer(ConfigProvider.fromEnv({ env: input.env ?? {} })),
64 );
65};
66
67it.effect("launches the default browser through the platform command", () => {
68 let spawned: ChildProcess.StandardCommand | undefined;
69 let didUnref = false;
70 return Effect.gen(function* () {
71 const launcher = yield* ExternalLauncher.ExternalLauncher;
72
73 yield* launcher.launchBrowser("https://example.com/some path");
74
75 assert.ok(spawned);
76 assert.equal(spawned.command, "xdg-open");
77 assert.deepEqual(spawned.args, ["https://example.com/some path"]);
78 assert.equal(spawned.options.detached, true);
79 assert.equal(didUnref, true);
80 }).pipe(
81 Effect.provide(
82 testLayer({
83 platform: "linux",
84 onSpawn: (command) => {
85 spawned = command;
86 },
87 onUnref: () => {
88 didUnref = true;
89 },
90 }),
91 ),
92 );
93});
94
95it.effect("launches an installed editor with platform-safe arguments", () =>
96 Effect.gen(function* () {
97 const fileSystem = yield* FileSystem.FileSystem;
98 const path = yield* Path.Path;
99 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
100 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
101
102 let spawned: ChildProcess.StandardCommand | undefined;
103 yield* Effect.gen(function* () {
104 const launcher = yield* ExternalLauncher.ExternalLauncher;
105 yield* launcher.launchEditor({
106 editor: "vscode",
107 cwd: "C:\\workspace with spaces\\src\\index.ts:12:4",
108 });
109 }).pipe(
110 Effect.provide(
111 testLayer({
112 platform: "win32",
113 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
114 resolveExecutable: (command) =>
115 command === "code" ? "C:\\Program Files\\Microsoft VS Code\\bin\\code.CMD" : command,
116 onSpawn: (command) => {
117 spawned = command;
118 },
119 }),
120 ),
121 );
122
123 assert.ok(spawned);
124 assert.equal(spawned.command, '^"C:\\Program^ Files\\Microsoft^ VS^ Code\\bin\\code.CMD^"');
125 assert.deepEqual(spawned.args, [
126 '^"--goto^"',
127 '^"C:\\workspace^ with^ spaces\\src\\index.ts:12:4^"',
128 ]);
129 assert.equal(spawned.options.shell, true);
130 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
131);
132
133it.effect("discovers editors through the service API", () =>
134 Effect.gen(function* () {
135 const fileSystem = yield* FileSystem.FileSystem;
136 const path = yield* Path.Path;
137 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
138 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
139 yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n");
140
141 const editors = yield* Effect.gen(function* () {
142 const launcher = yield* ExternalLauncher.ExternalLauncher;
143 return yield* launcher.resolveAvailableEditors();
144 }).pipe(
145 Effect.provide(
146 testLayer({
147 platform: "win32",
148 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
149 }),
150 ),
151 );
152
153 assert.equal(editors.includes("vscode"), true);
154 assert.equal(editors.includes("file-manager"), true);
155 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
156);
157
158it.effect("rejects unknown editors through the service API", () =>
159 Effect.gen(function* () {
160 const launcher = yield* ExternalLauncher.ExternalLauncher;
161 const error = yield* launcher
162 .launchEditor({ editor: "missing-editor" as never, cwd: "/tmp/workspace" })
163 .pipe(Effect.flip);
164 assert.instanceOf(error, ExternalLauncher.ExternalLauncherUnknownEditorError);
165 assert.equal(error.editor, "missing-editor");
166 assert.equal(error.message, "Unknown editor: missing-editor");
167 }).pipe(Effect.provide(testLayer({ platform: "linux", env: { PATH: "" } }))),
168);