From b3461bdcc0462bda7fad46b02f8307a0bc1166cd Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 20 Aug 2019 12:48:10 +0400 Subject: Added Main Reloader Signed-off-by: Valentin Popov --- src/mainReloader.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++-- src/rendererReloader.ts | 4 +++- 2 files changed, 47 insertions(+), 3 deletions(-) (limited to 'src') 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(); }); }); -- cgit v1.2.3