From 847dffebd037e46d377d3170694cb2fb477ff958 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/fs.js | 93 +++++++++++++++++++++++++++++++++++++++++--------- newton/main.js | 46 +++++++++++++++++-------- package.json | 3 ++ 3 files changed, 110 insertions(+), 32 deletions(-) diff --git a/newton/fs.js b/newton/fs.js index 8e0fc95..31a2dff 100644 --- a/newton/fs.js +++ b/newton/fs.js @@ -1,7 +1,12 @@ const { dialog } = require('electron'); const path = require('node:path'); +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 fs = require('node:fs'); -const os = require('os') +const os = require('os'); const chokidar = require('chokidar'); @@ -9,9 +14,13 @@ 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"); +const IPC_SERVER_IP = "127.0.0.1"; let window = null; let watcher = null; +let ipcServer = null; +let ipcServerPort = "4563"; +let ipcServerURL = `http://${IPC_SERVER_IP}:${ipcServerPort}`; const getIconPath = () => { @@ -43,10 +52,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 +107,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) => { @@ -125,6 +126,61 @@ const closeFile = (fpath) => { unwatchFile(fpath); } +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 = { newtonFs: { @@ -136,6 +192,9 @@ module.exports = { getFileContents: getFileContents, getLspConfigData: getLspConfigData, getSettingsConfigData: getSettingsConfigData, + loadIPCServer: loadIPCServer, + isIPCServerUp: isIPCServerUp, + sendFilesToIPC: sendFilesToIPC, setWindow: setWindow, loadFilesWatcher: loadFilesWatcher, unwatchFile: unwatchFile, diff --git a/newton/main.js b/newton/main.js index 3f8bc1c..1e6a0ca 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,41 @@ 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.fs.isIPCServerUp() ) { + newton.fs.loadIPCServer(); + } else { + console.log("IPCServer: Is loaded... sending files to existing app."); + await newton.fs.sendFilesToIPC( + newton.args.getArgs() + ); + app.quit(); + } + + newton.settings.loadsettings(); + newton.fs.loadFilesWatcher(); + + loadHandlers(); + createWindow(); + newton.fs.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"