UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

52 lines 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const signalExit = require("signal-exit"); const chalk_1 = require("chalk"); const alwayscli_1 = require("@alwaysai/alwayscli"); exports.PROCESS_EXITED_WITH_NON_ZERO_STATUS_CODE = 'Process exited with non-zero status code'; async function run(cmd) { return new Promise((resolve, reject) => { const child = child_process_1.spawn(cmd.exe, cmd.args || [], { cwd: cmd.cwd }); signalExit(() => { child.kill(); }); if (cmd.input) { cmd.input.pipe(child.stdin); } // TODO: Limit buffer size const outChunks = []; const allChunks = []; // stderr and stdout child.stdout.on('data', chunk => { outChunks.push(chunk); allChunks.push(chunk); }); child.stderr.on('data', chunk => { allChunks.push(chunk); }); child.on('error', err => { reject(err); }); child.on('close', code => { if (code === 0) { const str = Buffer.concat(outChunks).toString('utf8'); if (str.endsWith('\n')) { resolve(str.slice(0, -1)); } else { resolve(str); } } else { let message = `${exports.PROCESS_EXITED_WITH_NON_ZERO_STATUS_CODE} ${chalk_1.default.bold(code.toString())}`; message += '\n\n'; message += `$ ${cmd.exe} ${cmd.args ? cmd.args.join(' ') : ''}`; message += '\n\n'; message += Buffer.concat(allChunks).toString('utf8'); reject(new alwayscli_1.TerseError(message)); } }); }); } exports.run = run; //# sourceMappingURL=run.js.map