diff options
author | Valentin Popov <info@valentineus.link> | 2019-08-20 11:48:10 +0300 |
---|---|---|
committer | Valentin Popov <info@valentineus.link> | 2019-08-20 11:48:10 +0300 |
commit | b3461bdcc0462bda7fad46b02f8307a0bc1166cd (patch) | |
tree | 73fef208d8c29139219775a137d6d771d43e6f9a | |
parent | 419f48c18d84ba3516124571ad8134bf304e65ab (diff) | |
download | electron-hot-reload-b3461bdcc0462bda7fad46b02f8307a0bc1166cd.tar.xz electron-hot-reload-b3461bdcc0462bda7fad46b02f8307a0bc1166cd.zip |
Added Main Reloader
Signed-off-by: Valentin Popov <info@valentineus.link>
-rw-r--r-- | src/mainReloader.ts | 46 | ||||
-rw-r--r-- | src/rendererReloader.ts | 4 |
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(); }); }); |