Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Effect's spawner has no windowsVerbatimArguments option.
2import * as NodeChildProcess from "node:child_process";
3
4import * as Cause from "effect/Cause";
5import * as Context from "effect/Context";
6import * as Effect from "effect/Effect";
7
8export 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. */
14export 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
30const spawnDetachedVerbatim: WindowsVerbatimSpawner = (command, args) =>
31 awaitDetachedSpawn(() =>
32 NodeChildProcess.spawn(command, args, {
33 detached: true,
34 stdio: "ignore",
35 windowsVerbatimArguments: true,
36 }),
37 );
38
39export const WindowsVerbatimSpawn = Context.Reference<WindowsVerbatimSpawner>(
40 "t3/process/WindowsVerbatimSpawn",
41 { defaultValue: () => spawnDetachedVerbatim },
42);