diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rendererReloader.ts | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/src/rendererReloader.ts b/src/rendererReloader.ts index 63178cf..36f485d 100644 --- a/src/rendererReloader.ts +++ b/src/rendererReloader.ts @@ -1,3 +1,46 @@ -type TRendererReloader = (paths: string[], ignored?: string[], handler?: (path: string) => void) => void; +import chokidar from "chokidar"; +// tslint:disable-next-line: no-implicit-dependencies +import { BrowserWindow } from "electron"; -export const rendererReloader: TRendererReloader = (paths, ignored, handler) => { }; +type TRendererReloader = ( + 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 rendererReloader: TRendererReloader = ( + 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); + } + + BrowserWindow.getAllWindows().forEach((window: BrowserWindow) => { + window.webContents.reloadIgnoringCache(); + }); + }); +}; |