Preliminary file watch logic added

This commit is contained in:
itdominator 2025-06-12 22:56:03 -05:00
parent 9d3eb76960
commit 7b498d0602
5 changed files with 45 additions and 7 deletions

View File

@ -5,13 +5,13 @@ const os = require('os')
const chokidar = require('chokidar');
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")
let window = null;
let watcher = null;
const getIconPath = () => {
@ -34,7 +34,9 @@ const getFileContents = (_path, useRelativePath = false) => {
if (!useRelativePath) {
return fs.readFileSync(_path, 'utf8');
} else {
return fs.readFileSync(path.join(__dirname, _path), 'utf8');
let fpath = path.join(__dirname, _path);
watcher.add(fpath);
return fs.readFileSync(fpath, 'utf8');
}
} catch(err) {
return `{"message": {"type": "error", "text": "Error: Could not read ${_path}"}}`;
@ -56,6 +58,7 @@ const saveFileAs = (content) => {
}
saveFile(response.filePath, content);
watcher.add(response.filePath);
});
}
@ -87,13 +90,40 @@ const openFiles = (startPath) => {
}
window.webContents.send('load-files', response.filePaths);
watcher.add(response.filePaths);
});
}
const setWindow = (win) => {
window = win;
}
const loadFilesWatcher = () => {
watcher = chokidar.watch([], {});
watcher.on('change', (fpath) => {
console.log("File (changed) : ", fpath);
}).on('unlink', (fpath) => {
console.log("File (unlinked) : ", fpath);
});
/*
watcher = chokidar.watch([], {
ignored: (path, stats) => stats?.isFile() && !path.endsWith('.js'), // only watch js files
persistent: true,
});
*/
}
const unwatchFile = async (fpath) => {
console.log("File (unwatch) : ", fpath);
await watcher.unwatch(fpath);
}
const closeFile = (fpath) => {
unwatchFile(fpath);
}
module.exports = {
@ -101,10 +131,13 @@ module.exports = {
openFiles: openFiles,
saveFile: saveFile,
saveFileAs: saveFileAs,
closeFile: closeFile,
getIconPath: getIconPath,
getFileContents: getFileContents,
getLspConfigData: getLspConfigData,
getSettingsConfigData: getSettingsConfigData,
setWindow: setWindow
setWindow: setWindow,
loadFilesWatcher: loadFilesWatcher,
unwatchFile: unwatchFile,
}
};

View File

@ -11,15 +11,17 @@ const { newton } = require('./app');
const loadHandlers = () => {
ipcMain.handle('getLspConfigData', (eve) => newton.fs.getLspConfigData());
ipcMain.handle('getFileContents', (eve, file) => newton.fs.getFileContents(file));
ipcMain.handle('getFileContents', (eve, path) => newton.fs.getFileContents(path));
ipcMain.handle('openFiles', (eve, startPath) => newton.fs.openFiles(startPath));
ipcMain.handle('saveFile', (eve, path, content) => newton.fs.saveFile(path, content));
ipcMain.handle('closeFile', (eve, path) => newton.fs.closeFile(path));
ipcMain.handle('saveFileAs', (eve, content) => newton.fs.saveFileAs(content));
}
app.whenReady().then(() => {
newton.args.loadArgs();
newton.fs.loadFilesWatcher();
loadHandlers();
newton.settings.loadsettings();

View File

@ -13,10 +13,11 @@ contextBridge.exposeInMainWorld('main', {
contextBridge.exposeInMainWorld('fs', {
getLspConfigData: () => ipcRenderer.invoke("getLspConfigData"),
getFileContents: (file) => ipcRenderer.invoke("getFileContents", file),
getFileContents: (path) => ipcRenderer.invoke("getFileContents", path),
openFiles: (startPath) => ipcRenderer.invoke("openFiles", startPath),
saveFile: (path, content) => ipcRenderer.invoke("saveFile", path, content),
saveFileAs: (content) => ipcRenderer.invoke("saveFileAs", content),
closeFile: (path) => ipcRenderer.invoke("closeFile", path),
getPathForFile: (file) => webUtils.getPathForFile(file),
onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, files) => callback(files)),
onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)),
});

View File

@ -94,6 +94,7 @@ export class EditorsComponent {
file.session.destroy();
this.files.delete(path);
window.fs.closeFile(path);
});
}

View File

@ -27,8 +27,9 @@ declare global {
openFiles: (arg0) => Promise<string>,
saveFile: (arg0: any, arg1: any) => Promise<string>,
saveFileAs: (arg0: any) => Promise<string>,
closeFile: (arg0: any) => Promise<string>,
getPathForFile: any,
onLoadFiles: (arg0: any) => Promise<string>,
}
}
}
}