File size: 934 Bytes
101858b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

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 
      };
    }
  }
}