Add watcher status and additional typing.

This commit is contained in:
Patrick Fic
2025-03-21 11:28:30 -07:00
parent 6da5822197
commit 14e7c64eab
19 changed files with 385 additions and 81 deletions

View File

@@ -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,
};