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