alwaysai
Version:
The alwaysAI command-line interface (CLI)
57 lines • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = exports.PROCESS_EXITED_WITH_NON_ZERO_STATUS_CODE = void 0;
const child_process_1 = require("child_process");
const signalExit = require("signal-exit");
const chalk = require("chalk");
const alwayscli_1 = require("@alwaysai/alwayscli");
const logger_1 = require("../../logger");
exports.PROCESS_EXITED_WITH_NON_ZERO_STATUS_CODE = 'Process exited with non-zero status code';
async function run(cmd) {
return new Promise((resolve, reject) => {
var _a;
const optString = cmd.args ? cmd.args.join(' ') : '';
logger_1.logger.debug(`${cmd.exe} ${optString}`);
const child = (0, child_process_1.spawn)(cmd.exe, (_a = cmd.args) !== null && _a !== void 0 ? _a : [], { 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 if (code !== null) {
let message = `${exports.PROCESS_EXITED_WITH_NON_ZERO_STATUS_CODE} ${chalk.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.CliTerseError(message));
}
});
});
}
exports.run = run;
//# sourceMappingURL=run.js.map