|
| 1 | +import { |
| 2 | + spawn, |
| 3 | + spawnSync, |
| 4 | + type ChildProcessWithoutNullStreams, |
| 5 | +} from "node:child_process"; |
| 6 | +import { Terminal } from "@xterm/headless"; |
| 7 | +import { join } from "path"; |
| 8 | +import { mkdtemp, rm } from "fs/promises"; |
| 9 | +import { tmpdir } from "os"; |
| 10 | + |
| 11 | +export interface RenderOptions { |
| 12 | + cols?: number; |
| 13 | + rows?: number; |
| 14 | + cwd?: string; |
| 15 | + env?: Record<string, string>; |
| 16 | + timeout?: number; |
| 17 | +} |
| 18 | + |
| 19 | +export interface TerminalInstance extends Disposable { |
| 20 | + getScreen(): string; |
| 21 | + getLine(index: number): string; |
| 22 | + getLines(): string[]; |
| 23 | + waitUntil( |
| 24 | + condition: (screen: string) => boolean, |
| 25 | + timeoutMs?: number |
| 26 | + ): Promise<void>; |
| 27 | + write(data: string): void; |
| 28 | + |
| 29 | + /** Underlying Node child process */ |
| 30 | + readonly child: ChildProcessWithoutNullStreams; |
| 31 | + |
| 32 | + /** Underlying xterm Terminal instance */ |
| 33 | + readonly terminal: Terminal; |
| 34 | +} |
| 35 | + |
| 36 | +class TerminalInstanceImpl implements TerminalInstance { |
| 37 | + public readonly child: ChildProcessWithoutNullStreams; |
| 38 | + public readonly terminal: Terminal; |
| 39 | + private disposed = false; |
| 40 | + private processExited = false; |
| 41 | + private defaultTimeoutMs; |
| 42 | + |
| 43 | + constructor(command: string, options: RenderOptions = {}) { |
| 44 | + const { |
| 45 | + cols = 80, |
| 46 | + rows = 24, |
| 47 | + cwd = process.cwd(), |
| 48 | + env = process.env as Record<string, string>, |
| 49 | + timeout = 10000, |
| 50 | + } = options; |
| 51 | + |
| 52 | + this.defaultTimeoutMs = timeout; |
| 53 | + |
| 54 | + // xterm.js headless terminal buffer (no DOM) |
| 55 | + this.terminal = new Terminal({ |
| 56 | + cols, |
| 57 | + rows, |
| 58 | + allowProposedApi: true, |
| 59 | + }); |
| 60 | + |
| 61 | + if (process.platform === "win32") { |
| 62 | + throw new Error("Windows is not supported"); |
| 63 | + } |
| 64 | + |
| 65 | + // Run the command under a PTY via `script(1)`: |
| 66 | + // script -qf -c "<cmd args...>" /dev/null |
| 67 | + // -q (quiet), -f (flush), -c (run command) — output goes to stdout. |
| 68 | + // This is a workaround for Bun not supporting node-pty. |
| 69 | + const argv = [ |
| 70 | + "-qf", |
| 71 | + "-c", |
| 72 | + `stty cols ${cols} rows ${rows}; exec ${command}`, |
| 73 | + "/dev/null", |
| 74 | + ]; |
| 75 | + const child = spawn("script", argv, { |
| 76 | + cwd, |
| 77 | + env, |
| 78 | + stdio: ["pipe", "pipe", "pipe"], // Node creates pipes for us |
| 79 | + }) as ChildProcessWithoutNullStreams; |
| 80 | + |
| 81 | + this.child = child; |
| 82 | + |
| 83 | + // Stream stdout → xterm |
| 84 | + child.stdout.setEncoding("utf8"); |
| 85 | + child.stdout.on("data", (chunk: string) => { |
| 86 | + this.terminal.write(chunk); |
| 87 | + }); |
| 88 | + |
| 89 | + // Mirror stderr to the terminal too |
| 90 | + child.stderr.setEncoding("utf8"); |
| 91 | + child.stderr.on("data", (chunk: string) => { |
| 92 | + this.terminal.write(chunk); |
| 93 | + }); |
| 94 | + |
| 95 | + child.on("exit", (code, signal) => { |
| 96 | + this.processExited = true; |
| 97 | + if (!this.disposed && code !== 0) { |
| 98 | + console.warn(`Process exited with code ${code}, signal ${signal}`); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + child.on("error", (err) => { |
| 103 | + console.error("Failed to spawn child process:", err); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + private findScript(): string | null { |
| 108 | + const r = spawnSync("which", ["script"], { encoding: "utf8" }); |
| 109 | + if (r.status === 0 && r.stdout.trim()) { |
| 110 | + return r.stdout.trim(); |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + getScreen(): string { |
| 116 | + const buffer = this.terminal.buffer.active; |
| 117 | + const lines: string[] = []; |
| 118 | + for (let i = 0; i < buffer.length; i++) { |
| 119 | + const line = buffer.getLine(i); |
| 120 | + if (line) lines.push(line.translateToString(true)); |
| 121 | + } |
| 122 | + return lines.join("\n"); |
| 123 | + } |
| 124 | + |
| 125 | + getLine(index: number): string { |
| 126 | + const buffer = this.terminal.buffer.active; |
| 127 | + const line = buffer.getLine(index); |
| 128 | + return line ? line.translateToString(true) : ""; |
| 129 | + } |
| 130 | + |
| 131 | + getLines(): string[] { |
| 132 | + const buffer = this.terminal.buffer.active; |
| 133 | + const out: string[] = []; |
| 134 | + for (let i = 0; i < buffer.length; i++) { |
| 135 | + const line = buffer.getLine(i); |
| 136 | + if (line) out.push(line.translateToString(true)); |
| 137 | + } |
| 138 | + return out; |
| 139 | + } |
| 140 | + |
| 141 | + async waitUntil( |
| 142 | + condition: (screen: string) => boolean, |
| 143 | + timeoutMs?: number |
| 144 | + ): Promise<void> { |
| 145 | + const pollInterval = 50; |
| 146 | + |
| 147 | + return new Promise((resolve, reject) => { |
| 148 | + let pollTimer: ReturnType<typeof setInterval> | null = null; |
| 149 | + let timeoutId: ReturnType<typeof setTimeout> | null = null; |
| 150 | + |
| 151 | + const cleanup = () => { |
| 152 | + if (pollTimer) clearInterval(pollTimer); |
| 153 | + if (timeoutId) clearTimeout(timeoutId); |
| 154 | + }; |
| 155 | + |
| 156 | + const check = () => { |
| 157 | + if (condition(this.getScreen())) { |
| 158 | + cleanup(); |
| 159 | + resolve(); |
| 160 | + return true; |
| 161 | + } |
| 162 | + return false; |
| 163 | + }; |
| 164 | + |
| 165 | + if (check()) return; |
| 166 | + |
| 167 | + timeoutId = setTimeout(() => { |
| 168 | + cleanup(); |
| 169 | + reject( |
| 170 | + new Error( |
| 171 | + `Timeout after ${timeoutMs}ms\n\nCurrent screen:\n${this.getScreen()}` |
| 172 | + ) |
| 173 | + ); |
| 174 | + }, timeoutMs ?? this.defaultTimeoutMs); |
| 175 | + |
| 176 | + pollTimer = setInterval(check, pollInterval); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + write(data: string): void { |
| 181 | + // Send keystrokes to the child’s stdin |
| 182 | + this.child.stdin.write(data); |
| 183 | + } |
| 184 | + |
| 185 | + [Symbol.dispose](): void { |
| 186 | + this.dispose(); |
| 187 | + } |
| 188 | + |
| 189 | + dispose(): void { |
| 190 | + if (this.disposed) return; |
| 191 | + this.disposed = true; |
| 192 | + |
| 193 | + try { |
| 194 | + // Politely end stdin; then kill if needed |
| 195 | + this.child.stdin.end(); |
| 196 | + } catch { |
| 197 | + /* ignore */ |
| 198 | + } |
| 199 | + |
| 200 | + try { |
| 201 | + this.child.kill(); |
| 202 | + } catch (e) { |
| 203 | + console.warn("Error killing child:", e); |
| 204 | + } |
| 205 | + |
| 206 | + try { |
| 207 | + this.terminal.dispose(); |
| 208 | + } catch (e) { |
| 209 | + console.warn("Error disposing terminal:", e); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +const pathToCliEntrypoint = join(import.meta.dirname, "..", "index.ts"); |
| 215 | +export const BLINK_COMMAND = `bun ${pathToCliEntrypoint}`; |
| 216 | + |
| 217 | +export function render( |
| 218 | + command: string, |
| 219 | + options?: RenderOptions |
| 220 | +): TerminalInstance { |
| 221 | + return new TerminalInstanceImpl(command, options); |
| 222 | +} |
| 223 | + |
| 224 | +export async function makeTmpDir(): Promise< |
| 225 | + AsyncDisposable & { path: string } |
| 226 | +> { |
| 227 | + const dirPath = await mkdtemp(join(tmpdir(), "blink-tmp-")); |
| 228 | + return { |
| 229 | + path: dirPath, |
| 230 | + [Symbol.asyncDispose](): Promise<void> { |
| 231 | + return rm(dirPath, { recursive: true }); |
| 232 | + }, |
| 233 | + }; |
| 234 | +} |
| 235 | + |
| 236 | +export const KEY_CODES = { |
| 237 | + ENTER: "\r", |
| 238 | + TAB: "\t", |
| 239 | + BACKSPACE: "\x08", |
| 240 | + DELETE: "\x7f", |
| 241 | + UP: "\x1b[A", |
| 242 | + DOWN: "\x1b[B", |
| 243 | + LEFT: "\x1b[D", |
| 244 | + RIGHT: "\x1b[C", |
| 245 | +} as const; |
0 commit comments