Newton-Editor/newton/fs.js

154 lines
4.5 KiB
JavaScript
Raw Normal View History

const { dialog } = require('electron');
const path = require('node:path');
const fs = require('node:fs');
const os = require('os');
const chokidar = require('chokidar');
2025-06-08 04:38:42 +00:00
const HOME_DIR = os.homedir();
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");
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-08 04:38:42 +00:00
const getIconPath = () => {
return path.join(CONFIG_PATH, "./icons/newton.png");
}
const getSettingsConfigData = () => {
return getFileContents(
SETTINGS_CONFIG_PATH,
useRelativePath = false,
watchFile = false
);
2025-06-08 04:38:42 +00:00
}
const getLspConfigData = () => {
return getFileContents(
LSP_CONFIG_PATH,
useRelativePath = true,
watchFile = false
).replaceAll("{user.home}", HOME_DIR);
2025-06-08 04:38:42 +00:00
}
const getFileContents = (_path, useRelativePath = false, watchFile = true) => {
console.log(`Getting Contents For: ${_path}`);
try {
if (useRelativePath)
return fs.readFileSync(
path.join(__dirname, _path),
'utf8'
);
if (watchFile)
watcher.add(_path);
return fs.readFileSync(_path, 'utf8');
} catch(err) {
return `{"message": {"type": "error", "text": "Error: Could not read ${_path}"}}`;
}
}
const setWindow = (win) => {
window = win;
}
const saveSettingsConfigData = (data) => {
saveFile(SETTINGS_CONFIG_PATH, data);
}
const saveFile = (fpath, content) => {
fs.writeFile(fpath, content, (err) => {
if (!err) return
console.error("An error ocurred writing to the file " + err.message);
});
}
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);
});
}
const openFiles = (startPath) => {
dialog.showOpenDialog(
{
title: "Open File(s):",
2025-06-08 04:38:42 +00:00
defaultPath: (startPath) ? startPath : HOME_DIR,
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"] },
],
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-13 03:56:03 +00:00
const loadFilesWatcher = () => {
watcher = chokidar.watch([], {});
watcher.on('change', (fpath) => {
console.debug("File (changed) : ", fpath);
window.webContents.send('file-changed', fpath);
2025-06-13 03:56:03 +00:00
}).on('unlink', (fpath) => {
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);
}
module.exports = {
newtonFs: {
setWindow: setWindow,
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,
getFileContents: getFileContents,
getLspConfigData: getLspConfigData,
2025-06-08 04:38:42 +00:00
getSettingsConfigData: getSettingsConfigData,
saveSettingsConfigData: saveSettingsConfigData,
2025-06-13 03:56:03 +00:00
loadFilesWatcher: loadFilesWatcher,
unwatchFile: unwatchFile,
}
};