Add watcher status and additional typing.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import chokidar, { FSWatcher } from "chokidar";
|
||||
import { Notification } from "electron";
|
||||
import { BrowserWindow, Notification } from "electron";
|
||||
import log from "electron-log/main";
|
||||
import path from "path";
|
||||
import errorTypeCheck from "../../util/errorTypeCheck";
|
||||
import store from "../store/store";
|
||||
import ipcTypes from "../../util/ipcTypes.json";
|
||||
import ImportJob from "../decoder/decoder";
|
||||
import store from "../store/store";
|
||||
|
||||
let watcher: FSWatcher;
|
||||
|
||||
@@ -67,6 +68,10 @@ async function StartWatcher(): Promise<boolean> {
|
||||
// })
|
||||
.on("error", function (error) {
|
||||
log.error("Error in Watcher", errorTypeCheck(error));
|
||||
// mainWindow.webContents.send(
|
||||
// ipcTypes.toRenderer.watcher.error,
|
||||
// errorTypeCheck(error)
|
||||
// );
|
||||
})
|
||||
.on("ready", onWatcherReady)
|
||||
.on("raw", function (event, path, details) {
|
||||
@@ -77,24 +82,37 @@ async function StartWatcher(): Promise<boolean> {
|
||||
return true;
|
||||
}
|
||||
|
||||
function removeWatcherPath(path: string): void {
|
||||
watcher.unwatch(path);
|
||||
log.debug(`Stopped watching path: ${path}`);
|
||||
}
|
||||
|
||||
function addWatcherPath(path: string | string[]): void {
|
||||
watcher.add(path);
|
||||
log.debug(`Started watching path: ${path}`);
|
||||
}
|
||||
|
||||
function onWatcherReady(): void {
|
||||
log.info("Watcher ready!");
|
||||
// const b = BrowserWindow.getAllWindows()[0];
|
||||
// b.webContents.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set.
|
||||
|
||||
new Notification({
|
||||
title: "Watcher Started",
|
||||
body: "Newly exported estimates will be automatically uploaded.",
|
||||
}).show();
|
||||
log.info("Confirmed watched paths:", watcher.getWatched());
|
||||
mainWindow.webContents.send(ipcTypes.toRenderer.watcher.started);
|
||||
}
|
||||
|
||||
async function StopWatcher(): Promise<boolean> {
|
||||
const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set.
|
||||
|
||||
if (watcher) {
|
||||
await watcher.close();
|
||||
log.info("Watcher stopped.");
|
||||
mainWindow.webContents.send(ipcTypes.toRenderer.watcher.stopped);
|
||||
|
||||
new Notification({
|
||||
title: "RPS Watcher Stopped",
|
||||
title: "Watcher Stopped",
|
||||
body: "Estimates will not be automatically uploaded.",
|
||||
}).show();
|
||||
return true;
|
||||
@@ -107,4 +125,10 @@ async function HandleNewFile(path): Promise<void> {
|
||||
log.log("Received a new file", path);
|
||||
}
|
||||
|
||||
export { StartWatcher, StopWatcher, watcher };
|
||||
export {
|
||||
StartWatcher,
|
||||
StopWatcher,
|
||||
watcher,
|
||||
removeWatcherPath,
|
||||
addWatcherPath,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user