UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

41 lines (32 loc) 931 B
import { spawn } from 'child_process'; import signalExit = require('signal-exit'); import { Readable } from 'node:stream'; import { Cmd } from '../types'; export function runStreaming(cmd: Cmd) { return new Promise<Readable>((resolve, reject) => { const child = spawn(cmd.exe, cmd.args ?? [], { cwd: cmd.cwd }); signalExit(() => { child.kill(); }); if (cmd.input) { cmd.input.pipe(child.stdin); } const errChunks: Buffer[] = []; function stderrChunkHandler(chunk: Buffer) { errChunks.push(chunk); } child.stderr.on('data', stderrChunkHandler); child.on('error', (err: any) => { if (err && !err.stderr) { err.stderr = Buffer.concat(errChunks).toString(); } reject(err); }); child.stdout.once('readable', () => { resolve(child.stdout); child.stderr.removeListener('data', stderrChunkHandler); }); }); }