1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();
});
};
|