2017-03-25 23:00:16 +03:00
|
|
|
import * as fs from 'fs-promise'
|
2017-04-11 03:22:48 +03:00
|
|
|
const { exec, spawn } = require('child-process-promise')
|
2017-03-25 23:00:16 +03:00
|
|
|
|
|
|
|
import { SessionOptions, SessionPersistenceProvider } from './api'
|
|
|
|
|
|
|
|
|
|
|
|
export class ScreenPersistenceProvider extends SessionPersistenceProvider {
|
2017-04-02 18:33:55 +03:00
|
|
|
async attachSession (recoveryId: any): Promise<SessionOptions> {
|
|
|
|
let lines = (await exec('screen -list')).stdout.split('\n')
|
|
|
|
let screenPID = lines
|
|
|
|
.filter(line => line.indexOf('.' + recoveryId) !== -1)
|
|
|
|
.map(line => parseInt(line.trim().split('.')[0]))[0]
|
|
|
|
|
|
|
|
if (!screenPID) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = (await exec(`ps -o pid --ppid ${screenPID}`)).stdout.split('\n')
|
|
|
|
let recoveredTruePID = parseInt(lines[1].split(/\s/).filter(x => !!x)[0])
|
2017-03-25 23:00:16 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
recoveryId,
|
2017-04-02 18:33:55 +03:00
|
|
|
recoveredTruePID,
|
2017-03-25 23:00:16 +03:00
|
|
|
command: 'screen',
|
|
|
|
args: ['-r', recoveryId],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 18:33:55 +03:00
|
|
|
async startSession (options: SessionOptions): Promise<any> {
|
2017-03-25 23:00:16 +03:00
|
|
|
let configPath = '/tmp/.termScreenConfig'
|
|
|
|
await fs.writeFile(configPath, `
|
|
|
|
escape ^^^
|
|
|
|
vbell off
|
|
|
|
term xterm-color
|
|
|
|
bindkey "^[OH" beginning-of-line
|
|
|
|
bindkey "^[OF" end-of-line
|
2017-04-05 11:50:02 +03:00
|
|
|
bindkey "\\027[?1049h" stuff ----alternate enter-----
|
|
|
|
bindkey "\\027[?1049l" stuff ----alternate leave-----
|
2017-03-25 23:00:16 +03:00
|
|
|
termcapinfo xterm* 'hs:ts=\\E]0;:fs=\\007:ds=\\E]0;\\007'
|
|
|
|
defhstatus "^Et"
|
|
|
|
hardstatus off
|
2017-04-05 11:50:02 +03:00
|
|
|
altscreen on
|
2017-03-25 23:00:16 +03:00
|
|
|
`, 'utf-8')
|
|
|
|
let recoveryId = `term-tab-${Date.now()}`
|
2017-04-02 18:33:55 +03:00
|
|
|
let args = ['-d', '-m', '-c', configPath, '-U', '-S', recoveryId, '--', options.command].concat(options.args || [])
|
|
|
|
await spawn('screen', args, {
|
|
|
|
cwd: options.cwd,
|
|
|
|
env: options.env || process.env,
|
|
|
|
})
|
|
|
|
return recoveryId
|
2017-03-25 23:00:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async terminateSession (recoveryId: string): Promise<void> {
|
|
|
|
await exec(`screen -S ${recoveryId} -X quit`)
|
|
|
|
}
|
|
|
|
}
|