now listening for file changes and hot reloading

feature/settings-window
Michael Ochmann 3 years ago
parent 5955e346be
commit e9ffdc8119
  1. 26
      src/Ation.js

@ -2,6 +2,7 @@
const {app, protocol, dialog} = require("electron"); const {app, protocol, dialog} = require("electron");
const path = require("path"); const path = require("path");
const fs = require("fs/promises"); const fs = require("fs/promises");
const fsn = require("fs");
const WindowManager = require("./WindowManager"); const WindowManager = require("./WindowManager");
const MainMenu = require("./MainMenu"); const MainMenu = require("./MainMenu");
@ -10,12 +11,14 @@ const {parser} = require("./Parser");
class Ation { class Ation {
windowManager; windowManager;
mainMenu; mainMenu;
fileToOpen; watcher;
currentFile;
constructor() { constructor() {
if (Ation.Instances > 0) if (Ation.Instances > 0)
throw new Error("Only one Instance of Ation possible"); throw new Error("Only one Instance of Ation possible");
this.fileToOpen = null; this.currentFile = "";
this.watcher = null;
this.windowManager = new WindowManager(this); this.windowManager = new WindowManager(this);
this.mainMenu = new MainMenu(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) if (!this.windowManager.mainWindow)
return null; return null;
@ -56,12 +59,21 @@ class Ation {
filePath = result.filePaths[0]; filePath = result.filePaths[0];
} }
const fileContents = await fs.readFile(filePath, {encoding : "utf-8"}); try {
const basePath = path.dirname(filePath); const fileContents = await fs.readFile(filePath, {encoding : "utf-8"});
const data = parser(fileContents); 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; Ation.Instances = 0;

Loading…
Cancel
Save