Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as PlatformError from "effect/PlatformError";
6import * as Scope from "effect/Scope";
7import * as Sink from "effect/Sink";
8import * as Redacted from "effect/Redacted";
9import * as Stream from "effect/Stream";
10import type * as ChildProcess from "effect/process/ChildProcess";
11import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
12import * as HostProcess from "@t3tools/shared/HostProcess";
13import { makePool } from "./windowsProcessWorker.ts";
14
15type Spawn = (
16 command: ChildProcess.StandardCommand,
17) => Effect.Effect<
18 Pick<ChildProcessSpawner.ChildProcessHandle, "stdin" | "stdout" | "stderr" | "exitCode">,
19 PlatformError.PlatformError,
20 Scope.Scope
21>;
22
23export const WindowsSpawn = Context.Reference<Spawn | undefined>("t3/WindowsSpawn", {
24 defaultValue: () => undefined,
25});
26
27export const layer = Layer.effect(
28 WindowsSpawn,
29 Effect.gen(function* () {
30 if ((yield* HostProcess.Platform) !== "win32") return undefined;
31 const fs = yield* FileSystem.FileSystem;
32 const pool = yield* Effect.acquireRelease(Effect.sync(makePool), (pool) =>
33 Effect.promise(() => pool.close()),
34 );
35 const error = (cause: unknown) =>
36 PlatformError.systemError({
37 _tag:
38 typeof cause === "object" && cause !== null && "code" in cause
39 ? cause.code === "ENOENT"
40 ? "NotFound"
41 : cause.code === "EACCES" || cause.code === "EPERM"
42 ? "PermissionDenied"
43 : "Unknown"
44 : "Unknown",
45 module: "ChildProcess",
46 method: "spawn",
47 description: cause instanceof Error ? cause.message : String(cause),
48 cause,
49 });
50 const spawn: Spawn = Effect.fn("WindowsSpawn.spawn")(function* (command) {
51 if (command.options.cwd !== undefined) yield* fs.access(command.options.cwd);
52 const job = yield* Effect.acquireRelease(
53 Effect.try({
54 try: () =>
55 pool.start({
56 command: command.command,
57 args: command.args.map((arg) =>
58 Redacted.isRedacted(arg) ? Redacted.value(arg) : arg,
59 ),
60 cwd: command.options.cwd,
61 env: Object.fromEntries(
62 Object.entries({
63 ...(command.options.env === undefined || command.options.extendEnv === true
64 ? process.env
65 : {}),
66 ...command.options.env,
67 }).map(([key, value]) => [
68 key,
69 Redacted.isRedacted(value) ? Redacted.value(value) : value,
70 ]),
71 ),
72 shell: command.options.shell ?? false,
73 detached: command.options.detached,
74 windowsHide: command.options.windowsHide ?? true,
75 }),
76 catch: error,
77 }),
78 (job) => Effect.promise(() => job.dispose()),
79 );
80 const streams = yield* Effect.tryPromise({ try: () => job.started, catch: error });
81 return {
82 stdin: Sink.fromWritableStream({ evaluate: () => streams.stdin, onError: error }),
83 stdout: Stream.fromReadableStream({ evaluate: () => streams.stdout, onError: error }),
84 stderr: Stream.fromReadableStream({ evaluate: () => streams.stderr, onError: error }),
85 exitCode: Effect.tryPromise({ try: () => job.exit, catch: error }).pipe(
86 Effect.map(ChildProcessSpawner.ExitCode),
87 ),
88 };
89 });
90 return spawn;
91 }),
92);