aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mainReloader.ts46
-rw-r--r--src/rendererReloader.ts4
2 files changed, 47 insertions, 3 deletions
diff --git a/src/mainReloader.ts b/src/mainReloader.ts
index 5bd0fed..5a9a71b 100644
--- a/src/mainReloader.ts
+++ b/src/mainReloader.ts
@@ -1,3 +1,45 @@
-type TMainReloader = (paths: string[], ignored?: string[], handler?: (path: string) => void) => void;
+import chokidar from "chokidar";
+// tslint:disable-next-line: no-implicit-dependencies
+import { app } from "electron";
-export const mainReloader: TMainReloader = (paths, ignored, handler) => { };
+type TMainReloader = (
+ paths: string | string[],
+ ignored?: RegExp | RegExp[],
+ handler?: (error: Error | undefined, path: string | undefined) => void,
+ options?: chokidar.WatchOptions,
+) => void;
+
+const ignoredDefault: RegExp = /(node_modules|bower_components)/;
+
+export const mainReloader: TMainReloader = (
+ paths: string | string[],
+ ignored?: RegExp | RegExp[],
+ handler?: (error: Error | undefined, path: string | undefined) => void,
+ options?: chokidar.WatchOptions,
+): void => {
+ const ignoredPaths: RegExp[] = [ignoredDefault];
+
+ if (typeof ignored === "object") {
+ ignoredPaths.concat(ignored);
+ }
+
+ const watcher: chokidar.FSWatcher = chokidar.watch(paths, {
+ ignored: ignoredPaths,
+ ...options,
+ });
+
+ watcher.on("error", (error: Error) => {
+ if (typeof handler === "function") {
+ handler(error, undefined);
+ }
+ });
+
+ watcher.on("change", (path: string) => {
+ if (typeof handler === "function") {
+ handler(undefined, path);
+ }
+
+ app.relaunch();
+ app.exit();
+ });
+};
diff --git a/src/rendererReloader.ts b/src/rendererReloader.ts
index 36f485d..213dad0 100644
--- a/src/rendererReloader.ts
+++ b/src/rendererReloader.ts
@@ -39,7 +39,9 @@ export const rendererReloader: TRendererReloader = (
handler(undefined, path);
}
- BrowserWindow.getAllWindows().forEach((window: BrowserWindow) => {
+ const windows: BrowserWindow[] = BrowserWindow.getAllWindows();
+
+ windows.forEach((window: BrowserWindow) => {
window.webContents.reloadIgnoringCache();
});
});