Added IPC for files loading from external
This commit is contained in:
parent
c9c020385d
commit
847dffebd0
93
newton/fs.js
93
newton/fs.js
@ -1,7 +1,12 @@
|
|||||||
const { dialog } = require('electron');
|
const { dialog } = require('electron');
|
||||||
const path = require('node:path');
|
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 fs = require('node:fs');
|
||||||
const os = require('os')
|
const os = require('os');
|
||||||
const chokidar = require('chokidar');
|
const chokidar = require('chokidar');
|
||||||
|
|
||||||
|
|
||||||
@ -9,9 +14,13 @@ const HOME_DIR = os.homedir();
|
|||||||
const BASE_PATH = '../build/app';
|
const BASE_PATH = '../build/app';
|
||||||
const CONFIG_PATH = path.join(HOME_DIR, "/.config/newton/");
|
const CONFIG_PATH = path.join(HOME_DIR, "/.config/newton/");
|
||||||
const SETTINGS_CONFIG_PATH = path.join(CONFIG_PATH, "/settings.json");
|
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 window = null;
|
||||||
let watcher = null;
|
let watcher = null;
|
||||||
|
let ipcServer = null;
|
||||||
|
let ipcServerPort = "4563";
|
||||||
|
let ipcServerURL = `http://${IPC_SERVER_IP}:${ipcServerPort}`;
|
||||||
|
|
||||||
|
|
||||||
const getIconPath = () => {
|
const getIconPath = () => {
|
||||||
@ -43,10 +52,14 @@ const getFileContents = (_path, useRelativePath = false) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setWindow = (win) => {
|
||||||
|
window = win;
|
||||||
|
}
|
||||||
|
|
||||||
const saveFile = (fpath, content) => {
|
const saveFile = (fpath, content) => {
|
||||||
fs.writeFile(fpath, content, (err) => {
|
fs.writeFile(fpath, content, (err) => {
|
||||||
if (!err) return
|
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 = () => {
|
const loadFilesWatcher = () => {
|
||||||
watcher = chokidar.watch([], {});
|
watcher = chokidar.watch([], {});
|
||||||
|
|
||||||
watcher.on('change', (fpath) => {
|
watcher.on('change', (fpath) => {
|
||||||
console.log("File (changed) : ", fpath);
|
console.debug("File (changed) : ", fpath);
|
||||||
}).on('unlink', (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) => {
|
const unwatchFile = async (fpath) => {
|
||||||
@ -125,6 +126,61 @@ const closeFile = (fpath) => {
|
|||||||
unwatchFile(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 = {
|
module.exports = {
|
||||||
newtonFs: {
|
newtonFs: {
|
||||||
@ -136,6 +192,9 @@ module.exports = {
|
|||||||
getFileContents: getFileContents,
|
getFileContents: getFileContents,
|
||||||
getLspConfigData: getLspConfigData,
|
getLspConfigData: getLspConfigData,
|
||||||
getSettingsConfigData: getSettingsConfigData,
|
getSettingsConfigData: getSettingsConfigData,
|
||||||
|
loadIPCServer: loadIPCServer,
|
||||||
|
isIPCServerUp: isIPCServerUp,
|
||||||
|
sendFilesToIPC: sendFilesToIPC,
|
||||||
setWindow: setWindow,
|
setWindow: setWindow,
|
||||||
loadFilesWatcher: loadFilesWatcher,
|
loadFilesWatcher: loadFilesWatcher,
|
||||||
unwatchFile: unwatchFile,
|
unwatchFile: unwatchFile,
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
try {
|
try {
|
||||||
require('electron-reloader')(module)
|
require('electron-reloader')(module)
|
||||||
} catch {}
|
} catch(error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
|
||||||
const { app, ipcMain } = require('electron');
|
const { app, ipcMain } = require('electron');
|
||||||
|
|
||||||
const { newton } = require('./app');
|
const { newton } = require('./app');
|
||||||
|
|
||||||
|
|
||||||
|
let window = null;
|
||||||
|
|
||||||
|
|
||||||
const loadHandlers = () => {
|
const loadHandlers = () => {
|
||||||
ipcMain.handle('getLspConfigData', (eve) => newton.fs.getLspConfigData());
|
ipcMain.handle('getLspConfigData', (eve) => newton.fs.getLspConfigData());
|
||||||
@ -18,28 +21,41 @@ const loadHandlers = () => {
|
|||||||
ipcMain.handle('saveFileAs', (eve, content) => newton.fs.saveFileAs(content));
|
ipcMain.handle('saveFileAs', (eve, content) => newton.fs.saveFileAs(content));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const createWindow = () => {
|
||||||
app.whenReady().then(() => {
|
window = newton.createWindow(
|
||||||
newton.args.loadArgs();
|
|
||||||
newton.fs.loadFilesWatcher();
|
|
||||||
loadHandlers();
|
|
||||||
|
|
||||||
newton.settings.loadsettings();
|
|
||||||
let window = newton.createWindow(
|
|
||||||
newton.args.getStartType(),
|
newton.args.getStartType(),
|
||||||
newton.args.debugMode(),
|
newton.args.debugMode(),
|
||||||
newton.args.getArgs(),
|
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);
|
newton.fs.setWindow(window);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.on('activate', () => {
|
app.on('activate', () => {
|
||||||
if (BrowserWindow.getAllWindows().length === 0) {
|
if (BrowserWindow.getAllWindows().length === 0) {
|
||||||
newton.createWindow(
|
createWindow();
|
||||||
newton.args.getStartType(),
|
|
||||||
newton.args.debugMode(),
|
|
||||||
newton.args.getArgs(),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -55,8 +55,11 @@
|
|||||||
"bootstrap": "5.3.6",
|
"bootstrap": "5.3.6",
|
||||||
"bootstrap-icons": "1.12.1",
|
"bootstrap-icons": "1.12.1",
|
||||||
"chokidar": "4.0.3",
|
"chokidar": "4.0.3",
|
||||||
|
"electron-fetch": "1.9.1",
|
||||||
"express": "4.18.2",
|
"express": "4.18.2",
|
||||||
|
"node-fetch": "3.3.2",
|
||||||
"rxjs": "7.8.0",
|
"rxjs": "7.8.0",
|
||||||
|
"socket.io": "4.8.1",
|
||||||
"tslib": "2.3.0",
|
"tslib": "2.3.0",
|
||||||
"uuid": "11.1.0",
|
"uuid": "11.1.0",
|
||||||
"zone.js": "0.15.0"
|
"zone.js": "0.15.0"
|
||||||
|
Loading…
Reference in New Issue
Block a user