sicksync
Version:
Don’t accept the available as the preferable. Go extra mile with extra speed.
79 lines (57 loc) • 2.62 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.RemoteHelper = undefined;
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _events = require('events');
var _util = require('../util');
var util = _interopRequireWildcard(_util);
var _events2 = require('../../conf/events');
var _text = require('../../conf/text');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const COMMAND_NOT_FOUND = ['sicksync not found', 'no sicksync in', 'command not found'];
class RemoteHelper extends _events.EventEmitter {
constructor(params) {
super();
this._secret = params.secret;
this._webSocketPort = params.websocketPort;
this._username = params.username;
this._hostname = params.hostname;
this._prefersEncrypted = params.prefersEncrypted || false;
this._debug = params.debug || false;
}
_startRemoteSicksync(ssh) {
const context = this;
ssh.stdin.write(['sicksync remote', '-s', context._secret, '-p', context._webSocketPort, context._prefersEncrypted ? '-e' : '', context._debug ? '-d' : '', '\n'].join(' '));
}
start() {
const context = this;
const bootSicksync = _lodash2.default.once(this._startRemoteSicksync.bind(this));
const ssh = util.shellIntoRemote(this._username + '@' + this._hostname);
ssh.stdout.on('data', data => {
const message = data.toString();
// Boot sicksync (once!)
bootSicksync(ssh);
// If we get a 'ready' flag back from the server, emit a ready event
if (_lodash2.default.includes(message, _text.SERVER_ON_READY)) {
return context.emit(_events2.REMOTE.READY);
}
// If the message contains the devboxes name, emit the message
if (_lodash2.default.includes(message, context._secret)) {
const cleanedMessage = message.replace(context._secret, '').replace('\n', '');
return context.emit(_events2.REMOTE.MESSAGE, cleanedMessage);
}
// Not found/Not installed
_lodash2.default.each(COMMAND_NOT_FOUND, notFoundText => {
/* istanbul ignore else */
if (_lodash2.default.includes(message, notFoundText)) {
context.emit(_events2.REMOTE.NOT_FOUND, message);
}
});
});
}
}
exports.RemoteHelper = RemoteHelper;
;