2016-09-21 06:27:11 -08:00
|
|
|
const {EventEmitter} = require('events');
|
2016-10-02 08:54:27 -08:00
|
|
|
const {StringDecoder} = require('string_decoder');
|
2016-09-21 06:27:11 -08:00
|
|
|
|
|
|
|
|
const {app} = require('electron');
|
2016-07-01 15:50:28 -08:00
|
|
|
const defaultShell = require('default-shell');
|
2016-09-21 06:27:11 -08:00
|
|
|
|
|
|
|
|
const {getDecoratedEnv} = require('./plugins');
|
|
|
|
|
const {productName, version} = require('./package');
|
2016-08-01 23:49:25 -08:00
|
|
|
const config = require('./config');
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const createNodePtyError = () =>
|
|
|
|
|
new Error(
|
|
|
|
|
'`node-pty` failed to load. Typically this means that it was built incorrectly. Please check the `readme.md` to more info.'
|
|
|
|
|
);
|
2016-11-11 08:18:04 -09:00
|
|
|
|
2016-07-13 13:06:05 -08:00
|
|
|
let spawn;
|
|
|
|
|
try {
|
2017-01-09 17:37:46 -09:00
|
|
|
spawn = require('node-pty').spawn;
|
2016-07-13 13:06:05 -08:00
|
|
|
} catch (err) {
|
2017-01-15 06:22:07 -09:00
|
|
|
throw createNodePtyError();
|
2016-07-13 13:06:05 -08:00
|
|
|
}
|
|
|
|
|
|
2016-08-01 23:49:25 -08:00
|
|
|
const envFromConfig = config.getConfig().env || {};
|
|
|
|
|
|
2016-06-30 22:01:04 -08:00
|
|
|
module.exports = class Session extends EventEmitter {
|
2016-09-21 06:27:11 -08:00
|
|
|
constructor({rows, cols: columns, cwd, shell, shellArgs}) {
|
2016-06-30 22:01:04 -08:00
|
|
|
super();
|
2017-09-10 05:35:10 -08:00
|
|
|
const baseEnv = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
process.env,
|
|
|
|
|
{
|
|
|
|
|
LANG: app.getLocale().replace('-', '_') + '.UTF-8',
|
|
|
|
|
TERM: 'xterm-256color',
|
|
|
|
|
TERM_PROGRAM: productName,
|
|
|
|
|
TERM_PROGRAM_VERSION: version
|
|
|
|
|
},
|
|
|
|
|
envFromConfig
|
|
|
|
|
);
|
2016-07-24 10:03:24 -08:00
|
|
|
|
2017-04-28 12:57:17 -08:00
|
|
|
// Electron has a default value for process.env.GOOGLE_API_KEY
|
|
|
|
|
// We don't want to leak this to the shell
|
|
|
|
|
// See https://github.com/zeit/hyper/issues/696
|
|
|
|
|
if (baseEnv.GOOGLE_API_KEY && process.env.GOOGLE_API_KEY === baseEnv.GOOGLE_API_KEY) {
|
|
|
|
|
delete baseEnv.GOOGLE_API_KEY;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-02 08:54:27 -08:00
|
|
|
const decoder = new StringDecoder('utf8');
|
|
|
|
|
|
2016-08-13 11:30:17 -08:00
|
|
|
const defaultShellArgs = ['--login'];
|
|
|
|
|
|
2016-11-11 08:18:04 -09:00
|
|
|
try {
|
|
|
|
|
this.pty = spawn(shell || defaultShell, shellArgs || defaultShellArgs, {
|
2016-12-07 05:28:41 -09:00
|
|
|
cols: columns,
|
2016-11-11 08:18:04 -09:00
|
|
|
rows,
|
|
|
|
|
cwd,
|
|
|
|
|
env: getDecoratedEnv(baseEnv)
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (/is not a function/.test(err.message)) {
|
2017-01-15 06:22:07 -09:00
|
|
|
throw createNodePtyError();
|
2016-11-11 08:18:04 -09:00
|
|
|
} else {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2017-01-21 09:07:08 -09:00
|
|
|
this.pty.on('data', data => {
|
2016-08-19 12:19:04 -08:00
|
|
|
if (this.ended) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-10-02 08:54:27 -08:00
|
|
|
this.emit('data', decoder.write(data));
|
2016-06-30 22:01:04 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.pty.on('exit', () => {
|
|
|
|
|
if (!this.ended) {
|
|
|
|
|
this.ended = true;
|
|
|
|
|
this.emit('exit');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-07-23 16:57:47 -08:00
|
|
|
this.shell = shell || defaultShell;
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
exit() {
|
2016-06-30 22:01:04 -08:00
|
|
|
this.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
write(data) {
|
2017-01-21 09:07:08 -09:00
|
|
|
this.pty.write(data);
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-11 08:18:04 -09:00
|
|
|
resize({cols, rows}) {
|
2016-06-30 22:01:04 -08:00
|
|
|
try {
|
2017-01-21 09:07:08 -09:00
|
|
|
this.pty.resize(cols, rows);
|
2016-06-30 22:01:04 -08:00
|
|
|
} catch (err) {
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-06-30 22:25:19 -08:00
|
|
|
console.error(err.stack);
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
destroy() {
|
2016-07-03 12:35:45 -08:00
|
|
|
try {
|
2016-11-11 08:18:04 -09:00
|
|
|
this.pty.kill();
|
2016-07-03 12:35:45 -08:00
|
|
|
} catch (err) {
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-07-03 12:35:45 -08:00
|
|
|
console.error('exit error', err.stack);
|
|
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
this.emit('exit');
|
|
|
|
|
this.ended = true;
|
|
|
|
|
}
|
|
|
|
|
};
|