2025-06-01 18:49:18 +00:00
|
|
|
const { dialog } = require('electron');
|
2025-06-13 01:31:08 +00:00
|
|
|
const path = require('node:path');
|
2025-06-14 07:07:12 +00:00
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const http = require('http');
|
|
|
|
const socketIO = require('socket.io');
|
|
|
|
const fetch = require('electron-fetch').default
|
2025-06-13 01:31:08 +00:00
|
|
|
const fs = require('node:fs');
|
2025-06-14 07:07:12 +00:00
|
|
|
const os = require('os');
|
2025-06-13 01:31:08 +00:00
|
|
|
const chokidar = require('chokidar');
|
2025-06-01 18:49:18 +00:00
|
|
|
|
|
|
|
|
2025-06-08 04:38:42 +00:00
|
|
|
const HOME_DIR = os.homedir();
|
2025-06-08 20:09:51 +00:00
|
|
|
const BASE_PATH = '../build/app';
|
2025-06-08 04:38:42 +00:00
|
|
|
const CONFIG_PATH = path.join(HOME_DIR, "/.config/newton/");
|
|
|
|
const SETTINGS_CONFIG_PATH = path.join(CONFIG_PATH, "/settings.json");
|
2025-06-14 07:07:12 +00:00
|
|
|
const LSP_CONFIG_PATH = path.join(BASE_PATH, "/resources/lsp-servers-config.json");
|
|
|
|
const IPC_SERVER_IP = "127.0.0.1";
|
2025-06-08 04:38:42 +00:00
|
|
|
let window = null;
|
2025-06-13 03:56:03 +00:00
|
|
|
let watcher = null;
|
2025-06-14 07:07:12 +00:00
|
|
|
let ipcServer = null;
|
|
|
|
let ipcServerPort = "4563";
|
|
|
|
let ipcServerURL = `http://${IPC_SERVER_IP}:${ipcServerPort}`;
|
2025-06-01 18:49:18 +00:00
|
|
|
|
|
|
|
|
2025-06-08 04:38:42 +00:00
|
|
|
const getIconPath = () => {
|
|
|
|
return path.join(CONFIG_PATH, "./icons/newton.png");
|
|
|
|
}
|
|
|
|
|
|
|
|
const getSettingsConfigData = () => {
|
|
|
|
return getFileContents(SETTINGS_CONFIG_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLspConfigData = () => {
|
|
|
|
const config = getFileContents(LSP_CONFIG_PATH, useRelativePath = true);
|
|
|
|
return config.replaceAll("{user.home}", HOME_DIR);
|
|
|
|
}
|
2025-06-01 18:49:18 +00:00
|
|
|
|
|
|
|
const getFileContents = (_path, useRelativePath = false) => {
|
|
|
|
console.log(`Getting Contents For: ${_path}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!useRelativePath) {
|
|
|
|
return fs.readFileSync(_path, 'utf8');
|
|
|
|
} else {
|
2025-06-13 03:56:03 +00:00
|
|
|
let fpath = path.join(__dirname, _path);
|
|
|
|
watcher.add(fpath);
|
|
|
|
return fs.readFileSync(fpath, 'utf8');
|
2025-06-01 18:49:18 +00:00
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
return `{"message": {"type": "error", "text": "Error: Could not read ${_path}"}}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-14 07:07:12 +00:00
|
|
|
const setWindow = (win) => {
|
|
|
|
window = win;
|
|
|
|
}
|
|
|
|
|
2025-06-01 18:49:18 +00:00
|
|
|
const saveFile = (fpath, content) => {
|
|
|
|
fs.writeFile(fpath, content, (err) => {
|
|
|
|
if (!err) return
|
2025-06-14 07:07:12 +00:00
|
|
|
console.error("An error ocurred writing to the file " + err.message);
|
2025-06-01 18:49:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const saveFileAs = (content) => {
|
|
|
|
dialog.showSaveDialog().then((response) => {
|
|
|
|
if (response.canceled) {
|
|
|
|
console.log("You didn't save the file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveFile(response.filePath, content);
|
2025-06-13 03:56:03 +00:00
|
|
|
watcher.add(response.filePath);
|
2025-06-01 18:49:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const openFiles = (startPath) => {
|
|
|
|
dialog.showOpenDialog(
|
|
|
|
{
|
|
|
|
title: "Open File(s):",
|
2025-06-08 04:38:42 +00:00
|
|
|
defaultPath: (startPath) ? startPath : HOME_DIR,
|
2025-06-01 18:49:18 +00:00
|
|
|
filters: [
|
2025-06-01 21:36:58 +00:00
|
|
|
{ name: "All Files", extensions: ["*"] },
|
|
|
|
{ name: "c", extensions: [".h", ".c"] },
|
|
|
|
{ name: "cpp", extensions: ["hpp", "cpp"] },
|
|
|
|
{ name: "html", extensions: ["js", "css", "scss", "html", "ts"] },
|
|
|
|
{ name: "java", extensions: ["java"] },
|
|
|
|
{ name: "python", extensions: ["py", "pyc"] },
|
|
|
|
{ name: "rust", extensions: ["r", "rc"] },
|
|
|
|
{ name: "text", extensions: ["txt", "log", "md"] },
|
|
|
|
{ name: "go", extensions: ["go"] },
|
2025-06-01 18:49:18 +00:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
'openFile',
|
|
|
|
'multiSelections'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
).then((response) => {
|
|
|
|
if (response.canceled) {
|
|
|
|
console.log("Canceled file open request...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.webContents.send('load-files', response.filePaths);
|
2025-06-13 03:56:03 +00:00
|
|
|
watcher.add(response.filePaths);
|
2025-06-01 18:49:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-06-13 03:56:03 +00:00
|
|
|
const loadFilesWatcher = () => {
|
|
|
|
watcher = chokidar.watch([], {});
|
|
|
|
|
|
|
|
watcher.on('change', (fpath) => {
|
2025-06-14 07:07:12 +00:00
|
|
|
console.debug("File (changed) : ", fpath);
|
2025-06-13 03:56:03 +00:00
|
|
|
}).on('unlink', (fpath) => {
|
2025-06-14 07:07:12 +00:00
|
|
|
console.debug("File (unlinked) : ", fpath);
|
2025-06-13 03:56:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const unwatchFile = async (fpath) => {
|
|
|
|
console.log("File (unwatch) : ", fpath);
|
|
|
|
await watcher.unwatch(fpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const closeFile = (fpath) => {
|
|
|
|
unwatchFile(fpath);
|
|
|
|
}
|
2025-06-01 18:49:18 +00:00
|
|
|
|
2025-06-14 07:07:12 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-06-01 18:49:18 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
newtonFs: {
|
|
|
|
openFiles: openFiles,
|
|
|
|
saveFile: saveFile,
|
|
|
|
saveFileAs: saveFileAs,
|
2025-06-13 03:56:03 +00:00
|
|
|
closeFile: closeFile,
|
2025-06-08 04:38:42 +00:00
|
|
|
getIconPath: getIconPath,
|
2025-06-01 18:49:18 +00:00
|
|
|
getFileContents: getFileContents,
|
|
|
|
getLspConfigData: getLspConfigData,
|
2025-06-08 04:38:42 +00:00
|
|
|
getSettingsConfigData: getSettingsConfigData,
|
2025-06-14 07:07:12 +00:00
|
|
|
loadIPCServer: loadIPCServer,
|
|
|
|
isIPCServerUp: isIPCServerUp,
|
|
|
|
sendFilesToIPC: sendFilesToIPC,
|
2025-06-13 03:56:03 +00:00
|
|
|
setWindow: setWindow,
|
|
|
|
loadFilesWatcher: loadFilesWatcher,
|
|
|
|
unwatchFile: unwatchFile,
|
2025-06-01 18:49:18 +00:00
|
|
|
}
|
|
|
|
};
|