-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPtyService.js
44 lines (33 loc) · 1.08 KB
/
PtyService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const os = require('os')
const pty = require('node-pty')
const {defaultReadPath} = require('./config')
class PTY {
constructor(socket) {
this.shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
this.ptyProcess = null;
this.socket = socket;
// init pty service
this.startPtyProcess();
}
startPtyProcess(){
this.ptyProcess = pty.spawn(this.shell, [], {
name: 'xterm-color',
cwd: defaultReadPath, // starting path of the terminal
env: process.env
})
// add a 'data' event listener
this.ptyProcess.on('data', data => {
// whenever terminal outputs any data, send data to socket.io client
this.sendToClient(data)
})
}
// send input data ( commands ) to pty
write(data){
this.ptyProcess.write(data)
}
sendToClient(data){
// emit data to socket.io client
this.socket.emit('output', data)
}
}
module.exports = PTY;