UNPKG

@k1ssh/qbot

Version:

QBot SDK is a set of APIs for creating and managing microservices, and Kubernetes deployments. This is the core package for QBot AI.

56 lines (55 loc) 1.69 kB
import { spawn } from 'child_process'; import * as path from 'path'; import { log } from './Util.js'; /** * A class to execute commands in a given directory. */ export class CommandExecutor { constructor(initialDir) { this.cwd = initialDir; } async execute(command) { return new Promise((resolve, reject) => { const [cmd, ...args] = command.split(' '); if (cmd === 'cd') { this.cwd = path.resolve(this.cwd, args[0]); resolve(''); return; } const process = spawn(cmd, args, { cwd: this.cwd, shell: true, }); let output = ''; let error = ''; process.stdout.on('data', (data) => { output += data.toString(); }); process.stderr.on('data', (data) => { error += data.toString(); }); process.on('close', (code) => { if (code === 0) { resolve(output.trim()); } else { reject(new Error(`Command failed with code ${code}: ${error}`)); } }); }); } async executeSequence(commands) { for (const command of commands) { log(`Executing command: ${command}`); try { const output = await this.execute(command); if (output) log(output); } catch (error) { log(`Error executing command '${command}':`, error); throw error; } } } }