"use strict"; // TODO: These should really be their own individual classes. Are // there enough commonalities to warrant a base class? const spawn = require('child_process').spawn; const EventEmitter = require('events').EventEmitter; module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmitter { constructor(command) { super(); var cmd = command[0], args = command.slice(1); this.childProcess = spawn(cmd, args); this.childProcess.on('error', function(err) { this.emit('process_error', err); }); this.childProcess.on('close', function(code, signal) { this.emit('close', code, signal); }); this.childProcess.stdout.on('data', (data) => { var s = data.toString(), lines = s.split(/\n/g); for (var i = 0, l = lines.length; i < l; i++) { this.emit('message', lines[i]); } }); this.childProcess.stderr.on('data', (data) => { this.emit('error', data); }); } kill() { this.childProcess.kill(); } send(message) { this.childProcess.stdin.write(message); } } // vim:set ts=2 sw=2 et: