From 805b997483aaf623c5ab4568e1913203fc850461 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sat, 14 Jun 2025 02:07:12 -0500 Subject: [PATCH] Added IPC for files loading from external --- newton/app.js | 2 ++ newton/fs.js | 37 ++++++++++------------- newton/ipc.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ newton/main.js | 47 +++++++++++++++++++---------- package.json | 3 ++ 5 files changed, 133 insertions(+), 37 deletions(-) create mode 100644 newton/ipc.js diff --git a/newton/app.js b/newton/app.js index c5f98da..a8791c8 100644 --- a/newton/app.js +++ b/newton/app.js @@ -6,6 +6,7 @@ const { systemTray } = require('./system-tray'); const { argsParser } = require('./args-parser'); const { settingsManager } = require('./settings-manager'); const { newtonFs } = require('./fs'); +const { newtonIPC } = require('./ipc'); const BASE_PATH = '../build/app'; @@ -85,5 +86,6 @@ module.exports = { args: argsParser, settings: settingsManager, fs: newtonFs, + ipc: newtonIPC, } }; \ No newline at end of file diff --git a/newton/fs.js b/newton/fs.js index 8e0fc95..f60ca02 100644 --- a/newton/fs.js +++ b/newton/fs.js @@ -1,15 +1,15 @@ -const { dialog } = require('electron'); -const path = require('node:path'); -const fs = require('node:fs'); -const os = require('os') -const chokidar = require('chokidar'); +const { dialog } = require('electron'); +const path = require('node:path'); +const fs = require('node:fs'); +const os = require('os'); +const chokidar = require('chokidar'); const HOME_DIR = os.homedir(); const BASE_PATH = '../build/app'; const CONFIG_PATH = path.join(HOME_DIR, "/.config/newton/"); const SETTINGS_CONFIG_PATH = path.join(CONFIG_PATH, "/settings.json"); -const LSP_CONFIG_PATH = path.join(BASE_PATH, "/resources/lsp-servers-config.json") +const LSP_CONFIG_PATH = path.join(BASE_PATH, "/resources/lsp-servers-config.json"); let window = null; let watcher = null; @@ -43,10 +43,14 @@ const getFileContents = (_path, useRelativePath = false) => { } } +const setWindow = (win) => { + window = win; +} + const saveFile = (fpath, content) => { fs.writeFile(fpath, content, (err) => { if (!err) return - console.error("An error ocurred writing to the file " + err.message) + console.error("An error ocurred writing to the file " + err.message); }); } @@ -94,26 +98,14 @@ const openFiles = (startPath) => { }); } - -const setWindow = (win) => { - window = win; -} - const loadFilesWatcher = () => { watcher = chokidar.watch([], {}); watcher.on('change', (fpath) => { - console.log("File (changed) : ", fpath); + console.debug("File (changed) : ", fpath); }).on('unlink', (fpath) => { - console.log("File (unlinked) : ", fpath); + console.debug("File (unlinked) : ", fpath); }); - - /* - watcher = chokidar.watch([], { - ignored: (path, stats) => stats?.isFile() && !path.endsWith('.js'), // only watch js files - persistent: true, - }); - */ } const unwatchFile = async (fpath) => { @@ -126,8 +118,10 @@ const closeFile = (fpath) => { } + module.exports = { newtonFs: { + setWindow: setWindow, openFiles: openFiles, saveFile: saveFile, saveFileAs: saveFileAs, @@ -136,7 +130,6 @@ module.exports = { getFileContents: getFileContents, getLspConfigData: getLspConfigData, getSettingsConfigData: getSettingsConfigData, - setWindow: setWindow, loadFilesWatcher: loadFilesWatcher, unwatchFile: unwatchFile, } diff --git a/newton/ipc.js b/newton/ipc.js new file mode 100644 index 0000000..32d2fce --- /dev/null +++ b/newton/ipc.js @@ -0,0 +1,81 @@ +const express = require('express'); +const bodyParser = require('body-parser'); +const http = require('http'); +const socketIO = require('socket.io'); +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}`; + + +const setWindow = (win) => { + window = win; +} + +const loadIPCServer = (fpath) => { + + const app = express(); + ipcServer = http.createServer(app); + const io = socketIO(ipcServer); + + app.use(bodyParser.json()); + app.use( + bodyParser.urlencoded({ + extended: true, + }), + ); + + app.get("/is-up", (req, res) => { + res.status(200).send('yes'); + }); + + app.post("/load-files", (req, res) => { + console.debug("Load File(s) : ", req.body); + + window.webContents.send('load-files', req.body); + res.status(200).send(''); + }); + + ipcServer.listen(ipcServerPort, () => { + console.debug(`IPCServer is up on port ${ipcServerPort}`); + }); + +} + +const isIPCServerUp = async () => { + const response = await fetch(`${ipcServerURL}/is-up`).catch((err) => { + console.debug(err); + return { + text: () => { + return "no"; + } + } + }); + + const body = await response.text(); + return (body == "yes") ? true : false; +} + +const sendFilesToIPC = async (files) => { + let request = { + method: "POST", + body: JSON.stringify(files), + headers: { 'Content-Type': 'application/json' } + }; + + await fetch(`${ipcServerURL}/load-files`, request); +} + + +module.exports = { + newtonIPC: { + setWindow: setWindow, + loadIPCServer: loadIPCServer, + isIPCServerUp: isIPCServerUp, + sendFilesToIPC: sendFilesToIPC, + } +}; diff --git a/newton/main.js b/newton/main.js index 3f8bc1c..54e120f 100644 --- a/newton/main.js +++ b/newton/main.js @@ -1,13 +1,16 @@ try { require('electron-reloader')(module) -} catch {} - +} catch(error) { + console.log(error); +} const { app, ipcMain } = require('electron'); const { newton } = require('./app'); +let window = null; + const loadHandlers = () => { ipcMain.handle('getLspConfigData', (eve) => newton.fs.getLspConfigData()); @@ -18,28 +21,42 @@ const loadHandlers = () => { ipcMain.handle('saveFileAs', (eve, content) => newton.fs.saveFileAs(content)); } - -app.whenReady().then(() => { - newton.args.loadArgs(); - newton.fs.loadFilesWatcher(); - loadHandlers(); - - newton.settings.loadsettings(); - let window = newton.createWindow( +const createWindow = () => { + window = newton.createWindow( newton.args.getStartType(), newton.args.debugMode(), newton.args.getArgs(), ); +} + + +app.whenReady().then(async () => { + newton.args.loadArgs(); + + if ( !await newton.ipc.isIPCServerUp() ) { + newton.ipc.loadIPCServer(); + } else { + console.log("IPCServer: Is loaded... sending files to existing app."); + await newton.ipc.sendFilesToIPC( + newton.args.getArgs() + ); + app.quit(); + } + + newton.settings.loadsettings(); + newton.fs.loadFilesWatcher(); + + loadHandlers(); + createWindow(); + newton.fs.setWindow(window); + newton.fs.ipc.setWindow(window); + }); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { - newton.createWindow( - newton.args.getStartType(), - newton.args.debugMode(), - newton.args.getArgs(), - ); + createWindow(); }; }); diff --git a/package.json b/package.json index 06a53c2..9f7660c 100644 --- a/package.json +++ b/package.json @@ -55,8 +55,11 @@ "bootstrap": "5.3.6", "bootstrap-icons": "1.12.1", "chokidar": "4.0.3", + "electron-fetch": "1.9.1", "express": "4.18.2", + "node-fetch": "3.3.2", "rxjs": "7.8.0", + "socket.io": "4.8.1", "tslib": "2.3.0", "uuid": "11.1.0", "zone.js": "0.15.0"