Open History to see the last commit
1import { createRequire } from "node:module";
2
3import { Effect, FileSystem, Layer, Path } from "effect";
4import { PtyAdapter } from "../Services/PTY.ts";
5import {
6 PtySpawnError,
7 type PtyAdapterShape,
8 type PtyExitEvent,
9 type PtyProcess,
10} from "../Services/PTY.ts";
11
12let didEnsureSpawnHelperExecutable = false;
13
14const resolveNodePtySpawnHelperPath = Effect.gen(function* () {
15 const requireForNodePty = createRequire(import.meta.url);
16 const path = yield* Path.Path;
17 const fs = yield* FileSystem.FileSystem;
18
19 const packageJsonPath = requireForNodePty.resolve("node-pty/package.json");
20 const packageDir = path.dirname(packageJsonPath);
21 const candidates = [
22 path.join(packageDir, "build", "Release", "spawn-helper"),
23 path.join(packageDir, "build", "Debug", "spawn-helper"),
24 path.join(packageDir, "prebuilds", `${process.platform}-${process.arch}`, "spawn-helper"),
25 ];
26
27 for (const candidate of candidates) {
28 if (yield* fs.exists(candidate)) {
29 return candidate;
30 }
31 }
32 return null;
33}).pipe(Effect.orElseSucceed(() => null));
34
35export const ensureNodePtySpawnHelperExecutable = Effect.fn(function* (explicitPath?: string) {
36 const fs = yield* FileSystem.FileSystem;
37 if (process.platform === "win32") return;
38 if (!explicitPath && didEnsureSpawnHelperExecutable) return;
39
40 const helperPath = explicitPath ?? (yield* resolveNodePtySpawnHelperPath);
41 if (!helperPath) return;
42 if (!explicitPath) {
43 didEnsureSpawnHelperExecutable = true;
44 }
45
46 if (!(yield* fs.exists(helperPath))) {
47 return;
48 }
49
50 // Best-effort: avoid FileSystem.stat in packaged mode where some fs metadata can be missing.
51 yield* fs.chmod(helperPath, 0o755).pipe(Effect.orElseSucceed(() => undefined));
52});
53
54class NodePtyProcess implements PtyProcess {
55 private readonly process: import("node-pty").IPty;
56
57 constructor(process: import("node-pty").IPty) {
58 this.process = process;
59 }
60
61 get pid(): number {
62 return this.process.pid;
63 }
64
65 write(data: string): void {
66 this.process.write(data);
67 }
68
69 resize(cols: number, rows: number): void {
70 this.process.resize(cols, rows);
71 }
72
73 kill(signal?: string): void {
74 this.process.kill(signal);
75 }
76
77 onData(callback: (data: string) => void): () => void {
78 const disposable = this.process.onData(callback);
79 return () => {
80 disposable.dispose();
81 };
82 }
83
84 onExit(callback: (event: PtyExitEvent) => void): () => void {
85 const disposable = this.process.onExit((event) => {
86 callback({
87 exitCode: event.exitCode,
88 signal: event.signal ?? null,
89 });
90 });
91 return () => {
92 disposable.dispose();
93 };
94 }
95}
96
97export const layer = Layer.effect(
98 PtyAdapter,
99 Effect.gen(function* () {
100 const fs = yield* FileSystem.FileSystem;
101 const path = yield* Path.Path;
102
103 const nodePty = yield* Effect.promise(() => import("node-pty"));
104
105 const ensureNodePtySpawnHelperExecutableCached = yield* Effect.cached(
106 ensureNodePtySpawnHelperExecutable().pipe(
107 Effect.provideService(FileSystem.FileSystem, fs),
108 Effect.provideService(Path.Path, path),
109 Effect.orElseSucceed(() => undefined),
110 ),
111 );
112
113 return {
114 spawn: Effect.fn(function* (input) {
115 yield* ensureNodePtySpawnHelperExecutableCached;
116 const ptyProcess = yield* Effect.try({
117 try: () =>
118 nodePty.spawn(input.shell, input.args ?? [], {
119 cwd: input.cwd,
120 cols: input.cols,
121 rows: input.rows,
122 env: input.env,
123 name: globalThis.process.platform === "win32" ? "xterm-color" : "xterm-256color",
124 }),
125 catch: (cause) =>
126 new PtySpawnError({
127 adapter: "node-pty",
128 message: cause instanceof Error ? cause.message : "Failed to spawn PTY process",
129 cause,
130 }),
131 });
132 return new NodePtyProcess(ptyProcess);
133 }),
134 } satisfies PtyAdapterShape;
135 }),
136);