UNPKG

@plastichub/osr-ai-tools

Version:

CLI and library for LLM tools

131 lines (130 loc) 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Helper = exports.Process = exports.STATUS = void 0; const index_1 = require("../../index"); const child_process_1 = require("child_process"); var STATUS; (function (STATUS) { STATUS[STATUS["OK"] = 0] = "OK"; STATUS[STATUS["ERROR"] = 1] = "ERROR"; STATUS[STATUS["PENDING"] = 2] = "PENDING"; })(STATUS = exports.STATUS || (exports.STATUS = {})); const fatalHandler = (message, fn) => { if (message.startsWith('fatal:')) { fn('\t\ ' + message); return true; } return false; }; const defaultFilter = (message) => { return message.length > 0 && message !== '\n' && message !== '\r' && message !== '\r\n' && !message.startsWith('Debugger attached') && !message.includes('NODE_TLS_REJECT_UNAUTHORIZED') && !message.includes('Waiting for the debugger to disconnect'); }; const subscribe = (signal, collector = () => { }) => { if (!signal || !signal.on) { return; } signal.on('message', (message) => index_1.logger.debug('message', message)); signal.on('error', (error) => index_1.logger.error('std-error', error)); signal.on('data', (data) => { /* const msg = data.toString().replace(ansiRegex(), "") if (!defaultFilter(msg)) { return } collector(msg)*/ process.stdout.write(data); }); }; const merge = (buffer, data) => buffer.concat(data); const hook = (child, resolve, reject, cmd, buffer = []) => { const collector = (data) => { buffer.push(data); }; //subscribe(child.stderr, collector) //process.stdin.pipe(child.stdin) debugger; child.on('exit', (code, signal) => { debugger; if (code) { resolve({ code: STATUS.ERROR, command: cmd, error: code, messages: buffer }); } else { resolve({ code: STATUS.OK, command: cmd, messages: buffer }); } }); return child; }; class Process { constructor(options = {}) { this.binary = ''; this.cwd = ''; this.args = ''; this.buffer = []; this.binary = options.binary || this.binary; this.cwd = options.cwd || process.cwd(); this.buffer = options.buffer || []; } async exec(command, args = []) { args = [command].concat(args); try { let cmd = `${this.binary} ${args.join(' ')}`; /* const p = new Promise<any>((resolve, reject) => { const p = exec(cmd, { cwd: this.cwd }) return hook(p, resolve, reject, this.binary + ' ' + args.join(' '), this.buffer) }) return p */ try { //stdio: ['pipe', 'pipe', 'pipe'], debugger; const p = new Promise((resolve, reject) => { const cp = (0, child_process_1.spawn)(cmd, args, { cwd: this.cwd, shell: true, stdio: 'inherit', env: { ...process.env }, }); return hook(cp, resolve, reject, cmd, this.buffer); }); return p; } catch (e) { index_1.logger.error('Error executing command', e); } } catch (e) { index_1.logger.error('Error executing command', e); } } } exports.Process = Process; class Helper { static async run(cwd, cmd, args, buffer = [], debug_stream = false) { debug_stream && index_1.logger.info(`Run ${cmd} in ${cwd}`, args); const gitProcess = new Process({ cwd, binary: cmd, buffer }); return gitProcess.exec('', args); } } exports.Helper = Helper;