diff --git a/newton/fs.js b/newton/fs.js index 70c7927..8e0fc95 100644 --- a/newton/fs.js +++ b/newton/fs.js @@ -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, } }; \ No newline at end of file diff --git a/newton/main.js b/newton/main.js index 1c99b05..3f8bc1c 100644 --- a/newton/main.js +++ b/newton/main.js @@ -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(); diff --git a/newton/preload.js b/newton/preload.js index cce0ff4..8940c92 100644 --- a/newton/preload.js +++ b/newton/preload.js @@ -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)), }); \ No newline at end of file diff --git a/src/app/editor/editors.component.ts b/src/app/editor/editors.component.ts index 6bbcc63..c4c080b 100644 --- a/src/app/editor/editors.component.ts +++ b/src/app/editor/editors.component.ts @@ -94,6 +94,7 @@ export class EditorsComponent { file.session.destroy(); this.files.delete(path); + window.fs.closeFile(path); }); } diff --git a/src/polyfills.ts b/src/polyfills.ts index 603d6a1..d04f1e9 100644 --- a/src/polyfills.ts +++ b/src/polyfills.ts @@ -27,8 +27,9 @@ declare global { openFiles: (arg0) => Promise, saveFile: (arg0: any, arg1: any) => Promise, saveFileAs: (arg0: any) => Promise, + closeFile: (arg0: any) => Promise, getPathForFile: any, onLoadFiles: (arg0: any) => Promise, } } -} +} \ No newline at end of file