local server control

This commit is contained in:
sawka 2022-10-30 12:53:39 -07:00
parent 9027edc0e1
commit 4078cb1f1e
4 changed files with 86 additions and 0 deletions

View File

@ -2,10 +2,20 @@ import * as electron from "electron";
import * as path from "path";
import * as fs from "fs";
import fetch from "node-fetch";
import * as child_process from "node:child_process";
import {debounce} from "throttle-debounce";
import {acquireSCElectronLock} from "./base";
import {handleJsonFetchResponse} from "./util";
// TODO fix these paths
const LocalServerPath = "/Users/mike/scripthaus/local-server";
const LocalServerCmd = `${LocalServerPath} > ~/scripthaus/local-server.log 2>&1`;
// const LocalServerCwd = "/Users/mike/scripthaus/";
const LocalServerCwd = "/Users/mike/work/gopath/src/github.com/scripthaus-dev/sh2-server";
let localServerProc = null;
let localServerShouldRestart = false;
let app = electron.app;
app.setName("ScriptHaus");
@ -166,6 +176,9 @@ function createMainWindow(clientData) {
win.webContents.on("will-navigate", shNavHandler);
win.on("resized", debounce(400, mainResizeHandler));
win.on("moved", debounce(400, mainResizeHandler));
win.on("close", () => {
MainWindow = null;
});
return win;
}
@ -232,6 +245,19 @@ electron.ipcMain.on("get-id", (event) => {
return;
});
electron.ipcMain.on("restart-server", (event) => {
if (localServerProc != null) {
localServerProc.kill();
localServerShouldRestart = true;
return;
}
else {
runLocalServer();
}
event.returnValue = true;
return;
});
function getContextMenu() : any {
let menu = new electron.Menu();
let menuItem = new electron.MenuItem({label: "Testing", click: () => console.log("click testing!")});
@ -252,6 +278,48 @@ function getClientData() {
});
}
function sendLSSC() {
if (MainWindow != null) {
if (localServerProc == null) {
MainWindow.webContents.send("local-server-status-change", false);
return;
}
MainWindow.webContents.send("local-server-status-change", true, localServerProc.pid);
}
}
function runLocalServer() {
console.log("trying to run local server");
let proc = child_process.spawn("/bin/bash", ["-c", LocalServerCmd], {
cwd: LocalServerCwd,
});
proc.on("exit", (e) => {
console.log("local-server exit", e);
localServerProc = null;
sendLSSC();
if (localServerShouldRestart) {
localServerShouldRestart = false;
this.runLocalServer();
}
});
proc.on("spawn", (e) => {
console.log("spawnned local-server");
localServerProc = proc;
setTimeout(() => {
sendLSSC();
}, 100);
});
proc.on("error", (e) => {
console.log("error running local-server", e);
})
proc.stdout.on("data", output => {
return;
});
proc.stderr.on("data", output => {
return;
});
}
electron.ipcMain.on("context-screen", (event, {screenId}, {x, y}) => {
console.log("context-screen", screenId);
let menu = getContextMenu();
@ -270,6 +338,7 @@ async function createMainWindowWrap() {
// ====== MAIN ====== //
(async () => {
runLocalServer();
await app.whenReady();
await createMainWindowWrap();
app.on('activate', () => {

View File

@ -2494,6 +2494,7 @@ class DisconnectedModal extends React.Component<{}, {}> {
@boundMethod
restartServer() {
GlobalModel.restartLocalServer();
}
@boundMethod

View File

@ -84,6 +84,7 @@ type KeyModsType = {
type ElectronApi = {
getId : () => string,
restartLocalServer : () => boolean,
onTCmd : (callback : (mods : KeyModsType) => void) => void,
onICmd : (callback : (mods : KeyModsType) => void) => void,
onLCmd : (callback : (mods : KeyModsType) => void) => void,
@ -95,6 +96,7 @@ type ElectronApi = {
onBracketCmd : (callback : (event : any, arg : {relative : number}, mods : KeyModsType) => void) => void,
onDigitCmd : (callback : (event : any, arg : {digit : number}, mods : KeyModsType) => void) => void,
contextScreen : (screenOpts : {screenId : string}, position : {x : number, y : number}) => void,
onLocalServerStatusChange : (callback : (status : boolean, pid : number) => void) => void,
};
function getApi() : ElectronApi {
@ -1527,6 +1529,7 @@ class Model {
termUsedRowsCache : Record<string, number> = {};
debugCmds : number = 0;
debugSW : OV<boolean> = mobx.observable.box(false);
localServerRunning : OV<boolean> = mobx.observable.box(false);
constructor() {
this.clientId = getApi().getId();
@ -1543,6 +1546,17 @@ class Model {
getApi().onMetaPageDown(this.onMetaPageDown.bind(this));
getApi().onBracketCmd(this.onBracketCmd.bind(this));
getApi().onDigitCmd(this.onDigitCmd.bind(this));
getApi().onLocalServerStatusChange(this.onLocalServerStatusChange.bind(this));
}
restartLocalServer() : void {
getApi().restartLocalServer();
}
onLocalServerStatusChange(status : boolean) : void {
mobx.action(() => {
this.localServerRunning.set(status);
})();
}
dumpStructure() : void {

View File

@ -2,6 +2,7 @@ let {contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld("api", {
getId: () => ipcRenderer.sendSync("get-id"),
restartLocalServer: () => ipcRenderer.sendSync("restart-server"),
onTCmd: (callback) => ipcRenderer.on("t-cmd", callback),
onICmd: (callback) => ipcRenderer.on("i-cmd", callback),
onLCmd: (callback) => ipcRenderer.on("l-cmd", callback),
@ -14,4 +15,5 @@ contextBridge.exposeInMainWorld("api", {
onBracketCmd: (callback) => ipcRenderer.on("bracket-cmd", callback),
onDigitCmd: (callback) => ipcRenderer.on("digit-cmd", callback),
contextScreen: (screenOpts, position) => ipcRenderer.send("context-screen", screenOpts, position),
onLocalServerStatusChange: (callback) => ipcRenderer.on("local-server-status-change", callback),
});