-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Terminal #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Terminal #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,53 @@ import type { | |
| GitListBranchesInput, | ||
| GitListBranchesResult, | ||
| GitRemoveWorktreeInput, | ||
| TerminalCommandInput, | ||
| TerminalCommandResult, | ||
| } from "@t3tools/contracts"; | ||
|
|
||
| export interface TerminalCommandInput { | ||
| command: string; | ||
| cwd: string; | ||
| timeoutMs?: number; | ||
| maxOutputBytes?: number; | ||
| } | ||
|
|
||
| export interface TerminalCommandResult { | ||
| stdout: string; | ||
| stderr: string; | ||
| code: number | null; | ||
| signal: NodeJS.Signals | null; | ||
| timedOut: boolean; | ||
| } | ||
|
|
||
| const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; | ||
|
|
||
| function appendChunkWithinLimit( | ||
| target: string, | ||
| currentBytes: number, | ||
| chunk: Buffer, | ||
| maxBytes: number, | ||
| ): { | ||
| next: string; | ||
| nextBytes: number; | ||
| truncated: boolean; | ||
| } { | ||
| const remaining = maxBytes - currentBytes; | ||
| if (remaining <= 0) { | ||
| return { next: target, nextBytes: currentBytes, truncated: true }; | ||
| } | ||
| if (chunk.length <= remaining) { | ||
| return { | ||
| next: `${target}${chunk.toString()}`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Low
🤖 Prompt for AI |
||
| nextBytes: currentBytes + chunk.length, | ||
| truncated: false, | ||
| }; | ||
| } | ||
| return { | ||
| next: `${target}${chunk.subarray(0, remaining).toString()}`, | ||
| nextBytes: currentBytes + remaining, | ||
| truncated: true, | ||
| }; | ||
| } | ||
|
|
||
| /** Spawn git directly with an argv array — no shell, no quoting needed. */ | ||
| function runGit(args: string[], cwd: string, timeoutMs = 30_000): Promise<TerminalCommandResult> { | ||
| return new Promise((resolve, reject) => { | ||
|
|
@@ -25,9 +68,13 @@ function runGit(args: string[], cwd: string, timeoutMs = 30_000): Promise<Termin | |
| stdio: ["ignore", "pipe", "pipe"], | ||
| }); | ||
|
|
||
| const maxOutputBytes = DEFAULT_MAX_OUTPUT_BYTES; | ||
| let stdout = ""; | ||
| let stderr = ""; | ||
| let stdoutBytes = 0; | ||
| let stderrBytes = 0; | ||
| let timedOut = false; | ||
| let outputTruncated = false; | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| timedOut = true; | ||
|
|
@@ -38,17 +85,26 @@ function runGit(args: string[], cwd: string, timeoutMs = 30_000): Promise<Termin | |
| }, timeoutMs); | ||
|
|
||
| child.stdout?.on("data", (chunk: Buffer) => { | ||
| stdout += chunk.toString(); | ||
| const appended = appendChunkWithinLimit(stdout, stdoutBytes, chunk, maxOutputBytes); | ||
| stdout = appended.next; | ||
| stdoutBytes = appended.nextBytes; | ||
| outputTruncated = outputTruncated || appended.truncated; | ||
| }); | ||
| child.stderr?.on("data", (chunk: Buffer) => { | ||
| stderr += chunk.toString(); | ||
| const appended = appendChunkWithinLimit(stderr, stderrBytes, chunk, maxOutputBytes); | ||
| stderr = appended.next; | ||
| stderrBytes = appended.nextBytes; | ||
| outputTruncated = outputTruncated || appended.truncated; | ||
| }); | ||
| child.on("error", (error) => { | ||
| clearTimeout(timeout); | ||
| reject(error); | ||
| }); | ||
| child.on("close", (code, signal) => { | ||
| clearTimeout(timeout); | ||
| if (outputTruncated) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Low
🤖 Prompt for AI |
||
| stderr = `${stderr}\n[output truncated at ${maxOutputBytes} bytes]`; | ||
| } | ||
| resolve({ stdout, stderr, code: code ?? null, signal: signal ?? null, timedOut }); | ||
| }); | ||
| }); | ||
|
|
@@ -57,6 +113,7 @@ function runGit(args: string[], cwd: string, timeoutMs = 30_000): Promise<Termin | |
| export async function runTerminalCommand( | ||
| input: TerminalCommandInput, | ||
| ): Promise<TerminalCommandResult> { | ||
| const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; | ||
| const shellPath = | ||
| process.platform === "win32" | ||
| ? (process.env.ComSpec ?? "cmd.exe") | ||
|
|
@@ -74,7 +131,10 @@ export async function runTerminalCommand( | |
|
|
||
| let stdout = ""; | ||
| let stderr = ""; | ||
| let stdoutBytes = 0; | ||
| let stderrBytes = 0; | ||
| let timedOut = false; | ||
| let outputTruncated = false; | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| timedOut = true; | ||
|
|
@@ -87,11 +147,17 @@ export async function runTerminalCommand( | |
| }, input.timeoutMs ?? 30_000); | ||
|
|
||
| child.stdout?.on("data", (chunk: Buffer) => { | ||
| stdout += chunk.toString(); | ||
| const appended = appendChunkWithinLimit(stdout, stdoutBytes, chunk, maxOutputBytes); | ||
| stdout = appended.next; | ||
| stdoutBytes = appended.nextBytes; | ||
| outputTruncated = outputTruncated || appended.truncated; | ||
| }); | ||
|
|
||
| child.stderr?.on("data", (chunk: Buffer) => { | ||
| stderr += chunk.toString(); | ||
| const appended = appendChunkWithinLimit(stderr, stderrBytes, chunk, maxOutputBytes); | ||
| stderr = appended.next; | ||
| stderrBytes = appended.nextBytes; | ||
| outputTruncated = outputTruncated || appended.truncated; | ||
| }); | ||
|
|
||
| child.on("error", (error) => { | ||
|
|
@@ -101,6 +167,9 @@ export async function runTerminalCommand( | |
|
|
||
| child.on("close", (code, signal) => { | ||
| clearTimeout(timeout); | ||
| if (outputTruncated) { | ||
| stderr = `${stderr}\n[output truncated at ${maxOutputBytes} bytes]`; | ||
| } | ||
| resolve({ | ||
| stdout, | ||
| stderr, | ||
|
|
@@ -138,8 +207,8 @@ export async function listGitBranches(input: GitListBranchesInput): Promise<GitL | |
| let currentPath: string | null = null; | ||
| for (const line of worktreeList.stdout.split("\n")) { | ||
| if (line.startsWith("worktree ")) { | ||
| currentPath = line.slice("worktree ".length); | ||
| if (!fs.existsSync(currentPath)) currentPath = null; | ||
| const candidatePath = line.slice("worktree ".length); | ||
| currentPath = fs.existsSync(candidatePath) ? candidatePath : null; | ||
| } else if (line.startsWith("branch refs/heads/") && currentPath) { | ||
| worktreeMap.set(line.slice("branch refs/heads/".length), currentPath); | ||
| } else if (line === "") { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import * as nodePty from "node-pty"; | ||
|
|
||
| export interface PtyExitEvent { | ||
| exitCode: number; | ||
| signal: number | null; | ||
| } | ||
|
|
||
| export interface PtyProcess { | ||
| readonly pid: number; | ||
| write(data: string): void; | ||
| resize(cols: number, rows: number): void; | ||
| kill(signal?: string): void; | ||
| onData(callback: (data: string) => void): () => void; | ||
| onExit(callback: (event: PtyExitEvent) => void): () => void; | ||
| } | ||
|
|
||
| export interface PtySpawnInput { | ||
| shell: string; | ||
| cwd: string; | ||
| cols: number; | ||
| rows: number; | ||
| env: NodeJS.ProcessEnv; | ||
| } | ||
|
|
||
| export interface PtyAdapter { | ||
| spawn(input: PtySpawnInput): PtyProcess; | ||
| } | ||
|
|
||
| class NodePtyProcess implements PtyProcess { | ||
| constructor(private readonly process: nodePty.IPty) {} | ||
|
|
||
| get pid(): number { | ||
| return this.process.pid; | ||
| } | ||
|
|
||
| write(data: string): void { | ||
| this.process.write(data); | ||
| } | ||
|
|
||
| resize(cols: number, rows: number): void { | ||
| this.process.resize(cols, rows); | ||
| } | ||
|
|
||
| kill(signal?: string): void { | ||
| this.process.kill(signal); | ||
| } | ||
|
|
||
| onData(callback: (data: string) => void): () => void { | ||
| const disposable = this.process.onData(callback); | ||
| return () => { | ||
| disposable.dispose(); | ||
| }; | ||
| } | ||
|
|
||
| onExit(callback: (event: PtyExitEvent) => void): () => void { | ||
| const disposable = this.process.onExit((event) => { | ||
| callback({ | ||
| exitCode: event.exitCode, | ||
| signal: event.signal ?? null, | ||
| }); | ||
| }); | ||
| return () => { | ||
| disposable.dispose(); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export class NodePtyAdapter implements PtyAdapter { | ||
| spawn(input: PtySpawnInput): PtyProcess { | ||
| const ptyProcess = nodePty.spawn(input.shell, [], { | ||
| cwd: input.cwd, | ||
| cols: input.cols, | ||
| rows: input.rows, | ||
| env: input.env, | ||
| name: globalThis.process.platform === "win32" ? "xterm-color" : "xterm-256color", | ||
| }); | ||
| return new NodePtyProcess(ptyProcess); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.