diff --git a/src/base.ts b/src/base.ts deleted file mode 100644 index 7eb11b4f..00000000 --- a/src/base.ts +++ /dev/null @@ -1,28 +0,0 @@ -import * as path from "path"; -import * as fs from "fs"; - -const HomeVarName = "HOME"; -const PromptHomeVarName = "PROMPT_HOME"; -const PromptLockFile = "prompt-electron.lock"; -const DBFileName = "prompt.db"; -const SessionsDirBaseName = "sessions"; -const RemotesDirBaseName = "remotes"; -const PromptDirName = "prompt"; - -function getPromptHomeDir() : string { - if (process.env[PromptHomeVarName]) { - return process.env[PromptHomeVarName]; - } - let homeDir = process.env[HomeVarName]; - if (!homeDir) { - homeDir = "/"; - } - return path.join(homeDir, PromptDirName); -} - -function getDBName() : string { - let promptHome = getPromptHomeDir(); - return path.join(promptHome, DBFileName); -} - -export {getPromptHomeDir, getDBName}; diff --git a/src/emain.ts b/src/emain.ts index 1ec65027..5def6cb3 100644 --- a/src/emain.ts +++ b/src/emain.ts @@ -51,7 +51,7 @@ if (isDev) { console.log("prompt-app PROMPT_DEV set"); } let app = electron.app; -app.setName("Prompt"); +app.setName((isDev ? "Prompt (Dev)" : "Prompt")); const DevLocalServerPath = "/Users/mike/prompt/local-server"; let localServerProc = null; let localServerShouldRestart = false; @@ -75,6 +75,10 @@ function getAppBasePath() { return path.dirname(__dirname); } +function getBaseHostPort() { + return "http://localhost:8080"; +} + function getLocalServerPath() { if (isDev) { return DevLocalServerPath @@ -103,7 +107,7 @@ function readAuthKey() { let authKeyFileName = path.join(homeDir, AuthKeyFile); if (!fs.existsSync(authKeyFileName)) { let authKeyStr = String(uuidv4()); - fs.writeFileSync(authKeyFileName, authKeyStr, 0o600); + fs.writeFileSync(authKeyFileName, authKeyStr, {mode: 0o600}); return authKeyStr; } let authKeyData = fs.readFileSync(authKeyFileName); @@ -256,7 +260,7 @@ function mainResizeHandler(e) { let bounds = win.getBounds(); console.log("resize/move", win.getBounds()); let winSize = {width: bounds.width, height: bounds.height, top: bounds.y, left: bounds.x}; - let url = "http://localhost:8080/api/set-winsize"; + let url = getBaseHostPort() + "/api/set-winsize"; let fetchHeaders = getFetchHeaders(); fetch(url, {method: "post", body: JSON.stringify(winSize), headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).catch((err) => { console.log("error setting winsize", err) @@ -312,6 +316,11 @@ electron.ipcMain.on("get-id", (event) => { return; }); +electron.ipcMain.on("get-isdev", (event) => { + event.returnValue = isDev; + return; +}); + electron.ipcMain.on("get-authkey", (event) => { event.returnValue = GlobalAuthKey; return; @@ -349,7 +358,7 @@ function getFetchHeaders() { } function getClientData() { - let url = "http://localhost:8080/api/get-client-data"; + let url = getBaseHostPort() + "/api/get-client-data"; let fetchHeaders = getFetchHeaders(); return fetch(url, {headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { if (data == null) { diff --git a/src/main.tsx b/src/main.tsx index 796a4e4c..11c86996 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -297,7 +297,7 @@ class LineCmd extends React.Component<{sw : ScreenWindow, line : LineType, width let {line} = this.props; this.rtnStateDiffFetched = true; let usp = new URLSearchParams({sessionid: line.sessionid, cmdid: line.cmdid}); - let url = "http://localhost:8080/api/rtnstate?" + usp.toString(); + let url = GlobalModel.getBaseHostPort() + "/api/rtnstate?" + usp.toString(); let fetchHeaders = GlobalModel.getFetchHeaders(); fetch(url, {headers: fetchHeaders}).then((resp) => { if (!resp.ok) { diff --git a/src/model.ts b/src/model.ts index d8bf4d98..d5acf1e3 100644 --- a/src/model.ts +++ b/src/model.ts @@ -92,6 +92,7 @@ type KeyModsType = { type ElectronApi = { getId : () => string, + getIsDev : () => boolean, getAuthKey : () => string, getLocalServerStatus : () => boolean, restartLocalServer : () => boolean, @@ -1552,11 +1553,13 @@ class Model { debugSW : OV = mobx.observable.box(false); localServerRunning : OV; authKey : string; + isDev : boolean; constructor() { this.clientId = getApi().getId(); + this.isDev = getApi().getIsDev(); this.authKey = getApi().getAuthKey(); - this.ws = new WSControl(this.clientId, this.authKey, (message : any) => this.runUpdate(message, false)); + this.ws = new WSControl(this.getBaseWsHostPort(), this.clientId, this.authKey, (message : any) => this.runUpdate(message, false)); this.ws.reconnect(); this.inputModel = new InputModel(); let isLocalServerRunning = getApi().getLocalServerStatus(); @@ -1575,6 +1578,14 @@ class Model { document.addEventListener("keydown", this.docKeyDownHandler.bind(this)); } + getBaseHostPort() : string { + return "http://localhost:8080"; + } + + getBaseWsHostPort() : string { + return "ws://localhost:8081"; + } + getFetchHeaders() : Record { return { "x-authkey": this.authKey, @@ -1994,7 +2005,7 @@ class Model { console.trace(); } } - let url = sprintf("http://localhost:8080/api/run-command"); + let url = sprintf(GlobalModel.getBaseHostPort() + "/api/run-command"); let fetchHeaders = this.getFetchHeaders(); fetch(url, {method: "post", body: JSON.stringify(cmdPk), headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { mobx.action(() => { @@ -2078,7 +2089,7 @@ class Model { _loadWindowAsync(newWin : Window) { this.windows.set(newWin.sessionId + "/" + newWin.windowId, newWin); let usp = new URLSearchParams({sessionid: newWin.sessionId, windowid: newWin.windowId}); - let url = new URL("http://localhost:8080/api/get-window?" + usp.toString()); + let url = new URL(GlobalModel.getBaseHostPort() + "/api/get-window?" + usp.toString()); let fetchHeaders = GlobalModel.getFetchHeaders(); fetch(url, {headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { if (data.data == null) { diff --git a/src/preload.js b/src/preload.js index acb16812..51eed608 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"), + getIsDev: () => ipcRenderer.sendSync("get-isdev"), getAuthKey: () => ipcRenderer.sendSync("get-authkey"), getLocalServerStatus: () => ipcRenderer.sendSync("local-server-status"), restartLocalServer: () => ipcRenderer.sendSync("restart-server"), diff --git a/src/term.ts b/src/term.ts index ee71b0b2..232fe72f 100644 --- a/src/term.ts +++ b/src/term.ts @@ -230,10 +230,10 @@ class TermWrap { _getReloadUrl() : string { if (this.termContext.remoteId != null) { - return sprintf("http://localhost:8080/api/remote-pty?remoteid=%s", this.termContext.remoteId); + return sprintf(GlobalModel.getBaseHostPort() + "/api/remote-pty?remoteid=%s", this.termContext.remoteId); } else { - return sprintf("http://localhost:8080/api/ptyout?sessionid=%s&cmdid=%s", this.termContext.sessionId, this.termContext.cmdId); + return sprintf(GlobalModel.getBaseHostPort() + "/api/ptyout?sessionid=%s&cmdid=%s", this.termContext.sessionId, this.termContext.cmdId); } } diff --git a/src/ws.ts b/src/ws.ts index a2b248ca..ef250f37 100644 --- a/src/ws.ts +++ b/src/ws.ts @@ -16,8 +16,10 @@ class WSControl { watchScreenId : string = null; wsLog : mobx.IObservableArray = mobx.observable.array([], {name: "wsLog"}) authKey : string; + baseHostPort : string; - constructor(clientId : string, authKey : string, messageCallback : (any) => void) { + constructor(baseHostPort : string, clientId : string, authKey : string, messageCallback : (any) => void) { + this.baseHostPort = baseHostPort; this.messageCallback = messageCallback; this.clientId = clientId; this.authKey = authKey; @@ -48,7 +50,7 @@ class WSControl { } this.log(sprintf("try reconnect (%s)", desc)); this.opening = true; - this.wsConn = new WebSocket("ws://localhost:8081/ws?clientid=" + this.clientId); + this.wsConn = new WebSocket(this.baseHostPort + "/ws?clientid=" + this.clientId); this.wsConn.onopen = this.onopen; this.wsConn.onmessage = this.onmessage; this.wsConn.onclose = this.onclose;