fire-ide
Version:
Web-based Integrated Development Environment for fire.js
64 lines (59 loc) • 1.55 kB
JavaScript
var spawn = require('child_process').spawn
var fs = require('fs')
var EventEmitter = require('events').EventEmitter
var spawned = module.exports.state = {
list: {},
count: 0
}
function Spawned(logPathPrefix, cmd, args, options) {
this.cmd = cmd
this.args = args
this.options = options
this.id = "S" + spawned.count
spawned.list[this.id] = this
this.running = false
spawned.count++
this.stderrPath = logPathPrefix + "-stderr"
this.stdoutPath = logPathPrefix + "-stdout"
}
Spawned.prototype = new EventEmitter()
Spawned.prototype.start = function() {
var self = this
this.running = true
this.child = spawn(this.cmd, this.args, this.options);
this.stdout = fs.createWriteStream(this.stdoutPath)
this.stderr = fs.createWriteStream(this.stderrPath)
this.child.stdout.setEncoding('utf8')
this.child.stdout.on('data', function (data) {
if(self.stdout.writable) {
self.stdout.write(data)
self.stdout.flush()
}
self.emit('stdout', data)
});
this.child.stderr.setEncoding('utf8')
this.child.stderr.on('data', function (data) {
self.stderr.write(data)
self.stderr.flush()
self.emit('stderr', data)
});
this.child.on('exit', function (code) {
self.finish()
self.emit('exit')
});
}
Spawned.prototype.finish = function() {
if(!this.running) return false
this.running = false
this.stdout.flush()
this.stderr.flush()
this.stdout.end()
this.stderr.end()
this.child.kill()
return true
}
Spawned.prototype.close = function() {
this.finish()
delete spawned.list[this.id]
}
module.exports.Spawned = Spawned