diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/index.ts | 7 | ||||
-rw-r--r-- | src/mainReloader.ts | 45 | ||||
-rw-r--r-- | src/rendererReloader.ts | 48 |
3 files changed, 100 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..82df114 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,7 @@ +import { mainReloader } from "./mainReloader"; +import { rendererReloader } from "./rendererReloader"; + +export { + mainReloader, + rendererReloader, +}; diff --git a/src/mainReloader.ts b/src/mainReloader.ts new file mode 100644 index 0000000..5a9a71b --- /dev/null +++ b/src/mainReloader.ts @@ -0,0 +1,45 @@ +import chokidar from "chokidar"; +// tslint:disable-next-line: no-implicit-dependencies +import { app } from "electron"; + +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 new file mode 100644 index 0000000..213dad0 --- /dev/null +++ b/src/rendererReloader.ts @@ -0,0 +1,48 @@ +import chokidar from "chokidar"; +// tslint:disable-next-line: no-implicit-dependencies +import { BrowserWindow } from "electron"; + +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); + } + + const windows: BrowserWindow[] = BrowserWindow.getAllWindows(); + + windows.forEach((window: BrowserWindow) => { + window.webContents.reloadIgnoringCache(); + }); + }); +}; |