diff --git a/src/Ation.js b/src/Ation.js index a77ab50..337a7cf 100644 --- a/src/Ation.js +++ b/src/Ation.js @@ -2,6 +2,7 @@ const {app, protocol, dialog} = require("electron"); const path = require("path"); const fs = require("fs/promises"); +const fsn = require("fs"); const WindowManager = require("./WindowManager"); const MainMenu = require("./MainMenu"); @@ -10,12 +11,14 @@ const {parser} = require("./Parser"); class Ation { windowManager; mainMenu; - fileToOpen; + watcher; + currentFile; constructor() { if (Ation.Instances > 0) throw new Error("Only one Instance of Ation possible"); - this.fileToOpen = null; + this.currentFile = ""; + this.watcher = null; this.windowManager = new WindowManager(this); this.mainMenu = new MainMenu(this); @@ -36,7 +39,7 @@ class Ation { }); } - async openFile(filePath = null) { + async openFile(filePath = null, change = false) { if (!this.windowManager.mainWindow) return null; @@ -56,12 +59,21 @@ class Ation { filePath = result.filePaths[0]; } - const fileContents = await fs.readFile(filePath, {encoding : "utf-8"}); - const basePath = path.dirname(filePath); - const data = parser(fileContents); + try { + const fileContents = await fs.readFile(filePath, {encoding : "utf-8"}); + const basePath = path.dirname(filePath); + const data = parser(fileContents); + if (!change) + this.currentFile = filePath; - this.windowManager.mainWindow.send("Ation::openFile", [basePath, data]); + this.watcher = fsn.watch(filePath, () => { + this.openFile(this.currentFile, true); + }); + this.windowManager.mainWindow.send("Ation::openFile", [basePath, data]); + } catch { + return; + } } } Ation.Instances = 0;