From 4078cb1f1e607338c908c7f76f8f0869639b5dc8 Mon Sep 17 00:00:00 2001 From: sawka Date: Sun, 30 Oct 2022 12:53:39 -0700 Subject: [PATCH] local server control --- src/emain.ts | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.tsx | 1 + src/model.ts | 14 ++++++++++ src/preload.js | 2 ++ 4 files changed, 86 insertions(+) diff --git a/src/emain.ts b/src/emain.ts index 50201d3d..c72e9ed9 100644 --- a/src/emain.ts +++ b/src/emain.ts @@ -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', () => { diff --git a/src/main.tsx b/src/main.tsx index 2464631b..d7b21631 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2494,6 +2494,7 @@ class DisconnectedModal extends React.Component<{}, {}> { @boundMethod restartServer() { + GlobalModel.restartLocalServer(); } @boundMethod diff --git a/src/model.ts b/src/model.ts index 986a1aa7..067f4c60 100644 --- a/src/model.ts +++ b/src/model.ts @@ -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 = {}; debugCmds : number = 0; debugSW : OV = mobx.observable.box(false); + localServerRunning : OV = 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 { diff --git a/src/preload.js b/src/preload.js index 2f690fce..314b0e26 100644 --- a/src/preload.js +++ b/src/preload.js @@ -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), });