Phillnet-2 / Tools /services /shellExecutionService.ts
ayjays132's picture
Upload 478 files
101858b verified
import * as child_process from 'node:child_process';
import { promisify } from 'node:util';
const exec = promisify(child_process.exec);
export interface ShellExecutionResult {
stdout: string;
stderr: string;
exitCode: number;
}
export interface ShellOutputEvent {
type: 'stdout' | 'stderr';
data: string;
}
export interface ShellExecutionConfig {
cwd?: string;
env?: Record<string, string>;
timeout?: number;
}
export class ShellExecutionService {
static async execute(command: string, options: ShellExecutionConfig = {}): Promise<ShellExecutionResult> {
try {
const { stdout, stderr } = await exec(command, options);
return { stdout: stdout.toString(), stderr: stderr.toString(), exitCode: 0 };
} catch (err: any) {
return {
stdout: err.stdout?.toString() || '',
stderr: err.stderr?.toString() || err.message,
exitCode: err.code || 1
};
}
}
}