2015-08-01 06:59:11 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var events = require('events');
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
var _ = require('lodash');
|
2015-08-19 05:11:13 +00:00
|
|
|
var pty = require('pty.js');
|
2015-08-03 00:27:05 +00:00
|
|
|
|
|
|
|
exports.Door = Door;
|
|
|
|
|
|
|
|
function Door(client, exeInfo) {
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
this.exeInfo = exeInfo;
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
this.exeInfo.encoding = this.exeInfo.encoding || 'cp437';
|
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
// exeInfo.cmd
|
|
|
|
// exeInfo.args[]
|
|
|
|
// exeInfo.env{}
|
|
|
|
// exeInfo.cwd
|
|
|
|
// exeInfo.encoding
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
require('util').inherits(Door, events.EventEmitter);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Door.prototype.run = function() {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
var door = pty.spawn(self.exeInfo.cmd, self.exeInfo.args, {
|
2015-08-03 00:27:05 +00:00
|
|
|
cols : self.client.term.termWidth,
|
|
|
|
rows : self.client.term.termHeight,
|
2015-08-04 05:11:17 +00:00
|
|
|
// :TODO: cwd
|
|
|
|
env : self.exeInfo.env,
|
2015-08-01 06:59:11 +00:00
|
|
|
});
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
// :TODO: can we pause the stream, write our own "Loading...", then on resume?
|
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
//door.pipe(self.client.term.output);
|
|
|
|
self.client.term.output.pipe(door);
|
|
|
|
|
|
|
|
// :TODO: do this with pluggable pipe/filter classes
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
door.setEncoding(this.exeInfo.encoding);
|
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
door.on('data', function doorData(data) {
|
|
|
|
self.client.term.write(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
door.on('close', function closed() {
|
2015-08-04 05:11:17 +00:00
|
|
|
self.client.term.output.unpipe(door);
|
|
|
|
self.client.term.output.resume();
|
|
|
|
});
|
|
|
|
|
|
|
|
door.on('exit', function exited(code) {
|
|
|
|
self.client.log.info( { code : code }, 'Door exited');
|
|
|
|
|
|
|
|
door.removeAllListeners();
|
|
|
|
|
|
|
|
self.emit('finished');
|
2015-08-03 00:27:05 +00:00
|
|
|
});
|
|
|
|
};
|