cursorai-errorprompter
Version:
AI-powered runtime error fixing for developers using Cursor
73 lines (72 loc) • 2.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessRunner = void 0;
const events_1 = require("events");
const execa_1 = require("execa");
class ProcessRunner extends events_1.EventEmitter {
constructor(options) {
super();
this.options = options;
this.isRunning = false;
}
async start() {
if (this.isRunning) {
throw new Error('Process is already running');
}
try {
const [cmd, ...args] = this.options.command.split(' ');
const childProcess = (0, execa_1.execa)(cmd, args, {
cwd: this.options.cwd,
env: this.options.env,
stdio: ['inherit', 'pipe', 'pipe'],
});
this.process = childProcess;
this.isRunning = true;
if (childProcess.stdout) {
childProcess.stdout.on('data', (data) => {
this.emit('stdout', data.toString());
});
}
if (childProcess.stderr) {
childProcess.stderr.on('data', (data) => {
const errorEvent = {
raw: data.toString(),
timestamp: Date.now(),
};
this.emit('error', errorEvent);
});
}
childProcess.on('exit', (code) => {
this.isRunning = false;
this.emit('exit', code);
});
childProcess.on('error', (error) => {
this.isRunning = false;
this.emit('processError', error);
});
}
catch (error) {
this.isRunning = false;
throw error;
}
}
async stop() {
if (!this.isRunning || !this.process) {
return;
}
try {
this.process.kill();
await this.process;
}
catch (error) {
// Ignore errors when stopping
}
finally {
this.isRunning = false;
}
}
isProcessRunning() {
return this.isRunning;
}
}
exports.ProcessRunner = ProcessRunner;