working on separate prompt-dev version

This commit is contained in:
sawka 2022-12-28 13:47:51 -08:00
parent cbf185a715
commit 4e7929ee63
7 changed files with 35 additions and 40 deletions

View File

@ -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};

View File

@ -51,7 +51,7 @@ if (isDev) {
console.log("prompt-app PROMPT_DEV set"); console.log("prompt-app PROMPT_DEV set");
} }
let app = electron.app; let app = electron.app;
app.setName("Prompt"); app.setName((isDev ? "Prompt (Dev)" : "Prompt"));
const DevLocalServerPath = "/Users/mike/prompt/local-server"; const DevLocalServerPath = "/Users/mike/prompt/local-server";
let localServerProc = null; let localServerProc = null;
let localServerShouldRestart = false; let localServerShouldRestart = false;
@ -75,6 +75,10 @@ function getAppBasePath() {
return path.dirname(__dirname); return path.dirname(__dirname);
} }
function getBaseHostPort() {
return "http://localhost:8080";
}
function getLocalServerPath() { function getLocalServerPath() {
if (isDev) { if (isDev) {
return DevLocalServerPath return DevLocalServerPath
@ -103,7 +107,7 @@ function readAuthKey() {
let authKeyFileName = path.join(homeDir, AuthKeyFile); let authKeyFileName = path.join(homeDir, AuthKeyFile);
if (!fs.existsSync(authKeyFileName)) { if (!fs.existsSync(authKeyFileName)) {
let authKeyStr = String(uuidv4()); let authKeyStr = String(uuidv4());
fs.writeFileSync(authKeyFileName, authKeyStr, 0o600); fs.writeFileSync(authKeyFileName, authKeyStr, {mode: 0o600});
return authKeyStr; return authKeyStr;
} }
let authKeyData = fs.readFileSync(authKeyFileName); let authKeyData = fs.readFileSync(authKeyFileName);
@ -256,7 +260,7 @@ function mainResizeHandler(e) {
let bounds = win.getBounds(); let bounds = win.getBounds();
console.log("resize/move", win.getBounds()); console.log("resize/move", win.getBounds());
let winSize = {width: bounds.width, height: bounds.height, top: bounds.y, left: bounds.x}; 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(); let fetchHeaders = getFetchHeaders();
fetch(url, {method: "post", body: JSON.stringify(winSize), headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).catch((err) => { fetch(url, {method: "post", body: JSON.stringify(winSize), headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).catch((err) => {
console.log("error setting winsize", err) console.log("error setting winsize", err)
@ -312,6 +316,11 @@ electron.ipcMain.on("get-id", (event) => {
return; return;
}); });
electron.ipcMain.on("get-isdev", (event) => {
event.returnValue = isDev;
return;
});
electron.ipcMain.on("get-authkey", (event) => { electron.ipcMain.on("get-authkey", (event) => {
event.returnValue = GlobalAuthKey; event.returnValue = GlobalAuthKey;
return; return;
@ -349,7 +358,7 @@ function getFetchHeaders() {
} }
function getClientData() { function getClientData() {
let url = "http://localhost:8080/api/get-client-data"; let url = getBaseHostPort() + "/api/get-client-data";
let fetchHeaders = getFetchHeaders(); let fetchHeaders = getFetchHeaders();
return fetch(url, {headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { return fetch(url, {headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => {
if (data == null) { if (data == null) {

View File

@ -297,7 +297,7 @@ class LineCmd extends React.Component<{sw : ScreenWindow, line : LineType, width
let {line} = this.props; let {line} = this.props;
this.rtnStateDiffFetched = true; this.rtnStateDiffFetched = true;
let usp = new URLSearchParams({sessionid: line.sessionid, cmdid: line.cmdid}); 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(); let fetchHeaders = GlobalModel.getFetchHeaders();
fetch(url, {headers: fetchHeaders}).then((resp) => { fetch(url, {headers: fetchHeaders}).then((resp) => {
if (!resp.ok) { if (!resp.ok) {

View File

@ -92,6 +92,7 @@ type KeyModsType = {
type ElectronApi = { type ElectronApi = {
getId : () => string, getId : () => string,
getIsDev : () => boolean,
getAuthKey : () => string, getAuthKey : () => string,
getLocalServerStatus : () => boolean, getLocalServerStatus : () => boolean,
restartLocalServer : () => boolean, restartLocalServer : () => boolean,
@ -1552,11 +1553,13 @@ class Model {
debugSW : OV<boolean> = mobx.observable.box(false); debugSW : OV<boolean> = mobx.observable.box(false);
localServerRunning : OV<boolean>; localServerRunning : OV<boolean>;
authKey : string; authKey : string;
isDev : boolean;
constructor() { constructor() {
this.clientId = getApi().getId(); this.clientId = getApi().getId();
this.isDev = getApi().getIsDev();
this.authKey = getApi().getAuthKey(); 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.ws.reconnect();
this.inputModel = new InputModel(); this.inputModel = new InputModel();
let isLocalServerRunning = getApi().getLocalServerStatus(); let isLocalServerRunning = getApi().getLocalServerStatus();
@ -1575,6 +1578,14 @@ class Model {
document.addEventListener("keydown", this.docKeyDownHandler.bind(this)); document.addEventListener("keydown", this.docKeyDownHandler.bind(this));
} }
getBaseHostPort() : string {
return "http://localhost:8080";
}
getBaseWsHostPort() : string {
return "ws://localhost:8081";
}
getFetchHeaders() : Record<string, string> { getFetchHeaders() : Record<string, string> {
return { return {
"x-authkey": this.authKey, "x-authkey": this.authKey,
@ -1994,7 +2005,7 @@ class Model {
console.trace(); console.trace();
} }
} }
let url = sprintf("http://localhost:8080/api/run-command"); let url = sprintf(GlobalModel.getBaseHostPort() + "/api/run-command");
let fetchHeaders = this.getFetchHeaders(); let fetchHeaders = this.getFetchHeaders();
fetch(url, {method: "post", body: JSON.stringify(cmdPk), headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { fetch(url, {method: "post", body: JSON.stringify(cmdPk), headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => {
mobx.action(() => { mobx.action(() => {
@ -2078,7 +2089,7 @@ class Model {
_loadWindowAsync(newWin : Window) { _loadWindowAsync(newWin : Window) {
this.windows.set(newWin.sessionId + "/" + newWin.windowId, newWin); this.windows.set(newWin.sessionId + "/" + newWin.windowId, newWin);
let usp = new URLSearchParams({sessionid: newWin.sessionId, windowid: newWin.windowId}); 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(); let fetchHeaders = GlobalModel.getFetchHeaders();
fetch(url, {headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { fetch(url, {headers: fetchHeaders}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => {
if (data.data == null) { if (data.data == null) {

View File

@ -2,6 +2,7 @@ let {contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld("api", { contextBridge.exposeInMainWorld("api", {
getId: () => ipcRenderer.sendSync("get-id"), getId: () => ipcRenderer.sendSync("get-id"),
getIsDev: () => ipcRenderer.sendSync("get-isdev"),
getAuthKey: () => ipcRenderer.sendSync("get-authkey"), getAuthKey: () => ipcRenderer.sendSync("get-authkey"),
getLocalServerStatus: () => ipcRenderer.sendSync("local-server-status"), getLocalServerStatus: () => ipcRenderer.sendSync("local-server-status"),
restartLocalServer: () => ipcRenderer.sendSync("restart-server"), restartLocalServer: () => ipcRenderer.sendSync("restart-server"),

View File

@ -230,10 +230,10 @@ class TermWrap {
_getReloadUrl() : string { _getReloadUrl() : string {
if (this.termContext.remoteId != null) { 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 { 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);
} }
} }

View File

@ -16,8 +16,10 @@ class WSControl {
watchScreenId : string = null; watchScreenId : string = null;
wsLog : mobx.IObservableArray<string> = mobx.observable.array([], {name: "wsLog"}) wsLog : mobx.IObservableArray<string> = mobx.observable.array([], {name: "wsLog"})
authKey : string; 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.messageCallback = messageCallback;
this.clientId = clientId; this.clientId = clientId;
this.authKey = authKey; this.authKey = authKey;
@ -48,7 +50,7 @@ class WSControl {
} }
this.log(sprintf("try reconnect (%s)", desc)); this.log(sprintf("try reconnect (%s)", desc));
this.opening = true; 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.onopen = this.onopen;
this.wsConn.onmessage = this.onmessage; this.wsConn.onmessage = this.onmessage;
this.wsConn.onclose = this.onclose; this.wsConn.onclose = this.onclose;