This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from urbit/allow-terminal-access
Allow terminal access
- Loading branch information
Showing
15 changed files
with
928 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import os from "os" | ||
import { IPty, spawn } from "node-pty"; | ||
import { BrowserWindow, ipcMain } from "electron"; | ||
import isDev from 'electron-is-dev'; | ||
declare const TERMINAL_WEBPACK_ENTRY: string; | ||
|
||
const shell = os.platform() === "win32" ? "powershell.exe" : "bash"; | ||
|
||
export interface Payload { | ||
ship: string; | ||
data: string; | ||
} | ||
|
||
interface TerminalProcessParams { | ||
ship: string; | ||
initialCommand?: string; | ||
exitCommand?: string; | ||
} | ||
|
||
class TerminalProcess { | ||
ship: string; | ||
initialCommand?: string; | ||
exitCommand?: string; | ||
pty: IPty; | ||
initialized: boolean; | ||
msgCount: number; | ||
window: BrowserWindow; | ||
|
||
constructor({ ship, initialCommand, exitCommand }: TerminalProcessParams) { | ||
this.ship = ship; | ||
this.initialCommand = initialCommand; | ||
this.exitCommand = exitCommand; | ||
this.initialized = false; | ||
this.msgCount = 0; | ||
this.window = new BrowserWindow({ | ||
title: `${ship} | Terminal`, | ||
height: 450, | ||
width: 800, | ||
backgroundColor: "#000000", | ||
webPreferences: { | ||
nodeIntegration: true | ||
} | ||
}); | ||
this.window.on('close', (event) => { | ||
event.preventDefault(); | ||
this.destroy(); | ||
}); | ||
this.window.webContents.on('did-finish-load', () => { | ||
isDev && console.log('loaded, sending ship') | ||
this.window.webContents.send('ship', ship) | ||
}) | ||
this.window.loadURL(TERMINAL_WEBPACK_ENTRY); | ||
} | ||
|
||
init(): void { | ||
if (this.initialized) { | ||
return; | ||
} | ||
|
||
this.initialized = true; | ||
isDev && console.log('initializing pty') | ||
this.pty = spawn(shell, [], { | ||
name: "xterm-color", | ||
cols: 80, | ||
rows: 30, | ||
cwd: process.env.HOME, | ||
env: process.env | ||
}); | ||
|
||
this.pty.onData(data => { | ||
//console.log('sending terminal data', data) | ||
try { | ||
this.window.webContents.send('terminal-incoming', { ship: this.ship, data }); | ||
} catch(err) { | ||
console.log(err) | ||
} | ||
|
||
this.msgCount++; | ||
}); | ||
|
||
this.pty.onExit(({ exitCode, signal}) => { | ||
this.destroy(); | ||
console.log('exiting with', exitCode, signal); | ||
}) | ||
|
||
this.runInitCommand(); | ||
} | ||
|
||
runInitCommand() { | ||
setTimeout(() => { | ||
if (this.initialCommand && this.msgCount >= 1) { | ||
this.pty.write(this.initialCommand + '\r'); | ||
} else { | ||
this.runInitCommand(); | ||
} | ||
}, 500); | ||
} | ||
|
||
write(key: string): void { | ||
this.pty.write(key); | ||
} | ||
|
||
destroy(): void { | ||
if (this.exitCommand) { | ||
this.pty.write(this.exitCommand) | ||
} | ||
|
||
try { | ||
this.pty.kill(); | ||
|
||
setTimeout(() => { | ||
this.window.destroy(); | ||
}, 100); | ||
} catch (err) { | ||
console.log('errored on destroy', err) | ||
} | ||
} | ||
} | ||
|
||
const procs = new Map<string, TerminalProcess>(); | ||
|
||
function withTerm(ship: string, cb: (term: TerminalProcess) => void) { | ||
const term = procs.get(ship); | ||
|
||
if (term) { | ||
cb(term); | ||
} | ||
} | ||
|
||
export function start(): void { | ||
ipcMain.on('terminal-create', (event, params: TerminalProcessParams) => { | ||
const term = new TerminalProcess(params); | ||
isDev && console.log('creating terminal') | ||
procs.set(params.ship, term); | ||
}) | ||
|
||
ipcMain.on('terminal-loaded', (event, ship) => { | ||
isDev && console.log('terminal loaded') | ||
withTerm(ship, term => term.init()); | ||
}) | ||
|
||
ipcMain.on('terminal-keystroke', (event, { ship, data }: Payload) => { | ||
withTerm(ship, term => term.write(data)) | ||
}) | ||
|
||
ipcMain.on('terminal-kill', (event, ship) => { | ||
withTerm(ship, term => { | ||
term.destroy(); | ||
procs.delete(ship); | ||
}); | ||
}) | ||
} |
Oops, something went wrong.