File size: 954 Bytes
13894da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const MAX_OUTPUT_LENGTH = 30000;
const TIMEOUT = 2 * 60 * 1000;

export async function executeBash(bashCommand: string): Promise<string> {
  try {
    const command = new Deno.Command('bash', {
      args: ['-c', bashCommand],
      cwd: Deno.cwd(),
      signal: AbortSignal.timeout(TIMEOUT),
      stdout: 'piped',
      stderr: 'piped',
    });

    const process = await command.output();
    const stdout = new TextDecoder().decode(process.stdout);
    const stderr = new TextDecoder().decode(process.stderr);

    return [`<stdout>`, stdout.slice(0, MAX_OUTPUT_LENGTH), `</stdout>`, `<stderr>`, stderr.slice(0, MAX_OUTPUT_LENGTH), `</stderr>`].join('\n');
  } catch (error) {
    if (error instanceof DOMException && (error.name === 'AbortError' || error.name === 'TimeoutError')) {
      const stderr = `Command timed out or was aborted after ${TIMEOUT}ms.`;
      return [`<stderr>`, stderr, `</stderr>`].join('\n');
    }
    throw error;
  }
}