The best way to control Claude Code, Codex and more from any computer or phone

main
ts 42 lines 1.4 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - Effect's spawner has no windowsVerbatimArguments option.
2 import * as NodeChildProcess from "node:child_process";
3
4 import * as Cause from "effect/Cause";
5 import * as Context from "effect/Context";
6 import * as Effect from "effect/Effect";
7
8 export type WindowsVerbatimSpawner = (
9 command: string,
10 args: ReadonlyArray<string>,
11 ) => Effect.Effect<void, Error>;
12
13 /** Succeeds once a detached child has started and no longer holds this process open; fails if it cannot start. */
14 export const awaitDetachedSpawn = (spawn: () => NodeChildProcess.ChildProcess) =>
15 Effect.callback<void, Error>((resume) => {
16 let child: NodeChildProcess.ChildProcess;
17 try {
18 child = spawn();
19 } catch (error) {
20 resume(Effect.fail(error instanceof Error ? error : new Cause.UnknownError(error)));
21 return;
22 }
23 child.once("error", (error) => resume(Effect.fail(error)));
24 child.once("spawn", () => {
25 child.unref();
26 resume(Effect.void);
27 });
28 });
29
30 const spawnDetachedVerbatim: WindowsVerbatimSpawner = (command, args) =>
31 awaitDetachedSpawn(() =>
32 NodeChildProcess.spawn(command, args, {
33 detached: true,
34 stdio: "ignore",
35 windowsVerbatimArguments: true,
36 }),
37 );
38
39 export const WindowsVerbatimSpawn = Context.Reference<WindowsVerbatimSpawner>(
40 "t3/process/WindowsVerbatimSpawn",
41 { defaultValue: () => spawnDetachedVerbatim },
42 );