2025-06-14 07:07:12 +00:00
|
|
|
const { dialog } = require('electron');
|
|
|
|
const path = require('node:path');
|
|
|
|
const fs = require('node:fs');
|
|
|
|
const os = require('os');
|
|
|
|
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");
|
2025-06-08 04:38:42 +00:00
|
|
|
let window = null;
|
2025-06-13 03:56:03 +00:00
|
|
|
let watcher = null;
|
2025-06-01 18:49:18 +00:00
|
|
|
|
|
|
|
|
2025-06-20 01:03:02 +00:00
|
|
|
|
2025-06-08 04:38:42 +00:00
|
|
|
const getIconPath = () => {
|
|
|
|
return path.join(CONFIG_PATH, "./icons/newton.png");
|
|
|
|
}
|
|
|
|
|
|
|
|
const getSettingsConfigData = () => {
|
2025-06-20 01:03:02 +00:00
|
|
|
return getFileContents(
|
|
|
|
SETTINGS_CONFIG_PATH,
|
|
|
|
useRelativePath = false,
|
|
|
|
watchFile = false
|
|
|
|
);
|
2025-06-08 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getLspConfigData = () => {
|
2025-06-20 01:03:02 +00:00
|
|
|
return getFileContents(
|
|
|
|
LSP_CONFIG_PATH,
|
|
|
|
useRelativePath = true,
|
|
|
|
watchFile = false
|
|
|
|
).replaceAll("{user.home}", HOME_DIR);
|
2025-06-08 04:38:42 +00:00
|
|
|
}
|
2025-06-01 18:49:18 +00:00
|
|
|
|
2025-06-20 01:03:02 +00:00
|
|
|
const getFileContents = (_path, useRelativePath = false, watchFile = true) => {
|
2025-06-01 18:49:18 +00:00
|
|
|
console.log(`Getting Contents For: ${_path}`);
|
|
|
|
|
|
|
|
try {
|
2025-06-20 01:03:02 +00:00
|
|
|
if (useRelativePath)
|
|
|
|
return fs.readFileSync(
|
|
|
|
path.join(__dirname, _path),
|
|
|
|
'utf8'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (watchFile)
|
|
|
|
watcher.add(_path);
|
|
|
|
|
|
|
|
return fs.readFileSync(_path, '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-19 05:57:55 +00:00
|
|
|
const saveSettingsConfigData = (data) => {
|
|
|
|
saveFile(SETTINGS_CONFIG_PATH, data);
|
|
|
|
}
|
|
|
|
|
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: ["*"] },
|
2025-06-20 02:00:32 +00:00
|
|
|
{ name: "All Sub Filters",
|
|
|
|
extensions: [
|
|
|
|
"h", "c", "hpp", "cpp", "js", "css", "scss", "html",
|
|
|
|
"ts", "java", "py", "pyc", "txt", "log", "md", "r",
|
|
|
|
"rc", "go"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{ 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: "Text", extensions: ["txt", "log", "md"] },
|
|
|
|
{ name: "Rust", extensions: ["r", "rc"] },
|
|
|
|
{ name: "Go", extensions: ["go"] }
|
2025-06-01 18:49:18 +00:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
'openFile',
|
|
|
|
'multiSelections'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
).then((response) => {
|
|
|
|
if (response.canceled) {
|
2025-06-20 02:00:32 +00:00
|
|
|
console.log("Canceled file(s) open request...");
|
2025-06-01 18:49:18 +00:00
|
|
|
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-20 01:03:02 +00:00
|
|
|
window.webContents.send('file-changed', fpath);
|
2025-06-13 03:56:03 +00:00
|
|
|
}).on('unlink', (fpath) => {
|
2025-06-20 01:03:02 +00:00
|
|
|
console.debug("File (deleted) : ", fpath);
|
|
|
|
window.webContents.send('file-deleted', 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
|
|
|
|
2025-06-01 18:49:18 +00:00
|
|
|
module.exports = {
|
|
|
|
newtonFs: {
|
2025-06-14 07:07:12 +00:00
|
|
|
setWindow: setWindow,
|
2025-06-01 18:49:18 +00:00
|
|
|
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-19 05:57:55 +00:00
|
|
|
saveSettingsConfigData: saveSettingsConfigData,
|
2025-06-13 03:56:03 +00:00
|
|
|
loadFilesWatcher: loadFilesWatcher,
|
|
|
|
unwatchFile: unwatchFile,
|
2025-06-01 18:49:18 +00:00
|
|
|
}
|
|
|
|
};
|