From 080cc22841640848acc830263b5383da36c0e8de Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Mon, 14 Jul 2025 23:00:36 -0500 Subject: [PATCH] Making IPC port kwarg configurable; improving kwarhs filter out --- newton/args-parser.js | 57 +++++++++++++++++++++++-------------------- newton/ipc.js | 17 +++++++++---- newton/main.js | 1 + package.json | 4 +-- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/newton/args-parser.js b/newton/args-parser.js index c1b9582..8db5994 100644 --- a/newton/args-parser.js +++ b/newton/args-parser.js @@ -1,9 +1,10 @@ const { app } = require('electron'); -let startType = "build"; -let isDebug = false; -let args = []; +let startType = "build"; +let ipcPort = "4563"; +let isDebug = false; +let args = []; @@ -17,6 +18,13 @@ const loadKWArgs = () => { console.log(startType); } + const hasIpcPort = app.commandLine.hasSwitch("ipc-port"); + if (hasIpcPort) { + ipcPort = app.commandLine.getSwitchValue("ipc-port"); + console.log("Has ipc-port switch..."); + console.log(ipcPort); + } + const hasDebug = app.commandLine.hasSwitch("app-debug"); if (hasDebug) { isDebug = app.commandLine.getSwitchValue("app-debug"); @@ -25,38 +33,29 @@ const loadKWArgs = () => { } } -const loadVArgs = () => { - console.log("\n\nStart VArgs:"); - +const filterOutLaunchAndKWArgs = () => { if ( process.argv[0].endsWith("electron") ) { process.argv = process.argv.slice(2); } - if ( - process.argv[0].endsWith("/newton") || - process.argv[0].endsWith(".AppImage") - ) { + do { process.argv = process.argv.slice(1); - } - - if ( process.argv.length > 0 && ( - process.argv[0].includes("--trace-warnings") || - process.argv[0].includes("--start-as") + } while ( + process.argv.length > 0 && + ( + process.argv[0].endsWith("/newton") || + process.argv[0].endsWith(".AppImage") || + process.argv[0].includes("--trace-warnings") || + process.argv[0].includes("--start-as") || + process.argv[0].includes("--ipc-port") ) - ) { - process.argv = process.argv.slice(1); - } - - if ( process.argv.length > 0 && ( - process.argv[0].includes("--trace-warnings") || - process.argv[0].includes("--start-as") - ) - ) { - process.argv = process.argv.slice(1); - } + ); +} +const loadVArgs = () => { + console.log("\n\nStart VArgs:"); args = process.argv; args.forEach((val, index, array) => { console.log(index + ': ' + val); @@ -67,6 +66,7 @@ const loadVArgs = () => { const loadArgs = () => { loadKWArgs(); + filterOutLaunchAndKWArgs(); loadVArgs(); } @@ -83,6 +83,10 @@ const getDebugMode = () => { return isDebug; } +const getIpcPort = () => { + return ipcPort; +} + module.exports = { argsParser: { @@ -90,5 +94,6 @@ module.exports = { getArgs: getArgs, getStartType: getStartType, getDebugMode: getDebugMode, + getIpcPort: getIpcPort, } }; \ No newline at end of file diff --git a/newton/ipc.js b/newton/ipc.js index 533bf4b..26e52e4 100644 --- a/newton/ipc.js +++ b/newton/ipc.js @@ -8,12 +8,17 @@ const fetch = require('electron-fetch').default const IPC_SERVER_IP = "127.0.0.1"; let window = null; let ipcServer = null; -let ipcServerPort = "4563"; -let ipcServerURL = `http://${IPC_SERVER_IP}:${ipcServerPort}`; +let ipcServerPort = ""; +let ipcServerURL = ""; const setWindow = (win) => { - window = win; + window = win; +} + +const configure = (ipcPort) => { + ipcServerPort = ipcPort; + ipcServerURL = `http://${IPC_SERVER_IP}:${ipcServerPort}`; } const loadIPCServer = (fpath) => { @@ -47,7 +52,8 @@ const loadIPCServer = (fpath) => { } const isIPCServerUp = async () => { - const response = await fetch(`${ipcServerURL}/is-up`).catch((err) => { + const response = await fetch(`${ipcServerURL}/is-up`) + .catch((err) => { console.debug("IPCServer (status) : Not up; okay to start."); return { text: () => { @@ -73,9 +79,10 @@ const sendFilesToIPC = async (files) => { module.exports = { newtonIPC: { - setWindow: setWindow, + configure: configure, loadIPCServer: loadIPCServer, isIPCServerUp: isIPCServerUp, sendFilesToIPC: sendFilesToIPC, + setWindow: setWindow } }; \ No newline at end of file diff --git a/newton/main.js b/newton/main.js index 346ce7f..d4f1343 100644 --- a/newton/main.js +++ b/newton/main.js @@ -80,6 +80,7 @@ app.whenReady().then(async () => { loadProcessSignalHandlers(); newton.args.loadArgs(); + newton.ipc.configure( newton.args.getIpcPort() ); if ( !await newton.ipc.isIPCServerUp() ) { newton.ipc.loadIPCServer(); } else { diff --git a/package.json b/package.json index 8577bda..f978862 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "main": "newton/main.js", "private": true, "scripts": { - "app": "ng build --base-href ./ && electron . --trace-warnings --start-as=build", - "electron-start": "electron . --trace-warnings --start-as=build", + "app": "ng build --base-href ./ && electron . --trace-warnings --start-as=build --ipc-port=4588", + "electron-start": "electron . --trace-warnings --start-as=build --ipc-port=4588", "electron-pack": "ng build --base-href ./ && electron-builder --dir", "electron-dist": "ng build --base-href ./ && electron-builder", "electron-dist-linux": "ng build --base-href ./ && electron-builder --linux deb zip AppImage",