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-11-20 18:48:32 +00:00
|
|
|
var pty = require('ptyw.js');
|
2015-11-27 04:27:57 +00:00
|
|
|
var decode = require('iconv-lite').decode;
|
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
|
|
|
|
|
2015-08-26 05:17:09 +00:00
|
|
|
}
|
2015-08-03 00:27:05 +00:00
|
|
|
|
|
|
|
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-11-27 04:27:57 +00:00
|
|
|
//encoding : self.client.term.outputEncoding,
|
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);
|
2015-11-27 04:27:57 +00:00
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
// :TODO: do this with pluggable pipe/filter classes
|
|
|
|
|
2015-11-20 18:48:32 +00:00
|
|
|
// :TODO: This causes an error to be thrown when e.g. 'cp437' is used due to Node buffer changes
|
2015-11-27 04:27:57 +00:00
|
|
|
//door.setEncoding(this.exeInfo.encoding);
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
door.on('data', function doorData(data) {
|
2015-11-27 04:27:57 +00:00
|
|
|
self.client.term.write(decode(data, self.client.term.outputEncoding));
|
2015-08-03 00:27:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
};
|