Add chokidar watcher settings.

This commit is contained in:
Patrick Fic
2025-03-12 16:06:22 -07:00
parent a01d4bfb44
commit 10368f8f9e
8 changed files with 195 additions and 10 deletions

110
src/main/watcher/watcher.ts Normal file
View File

@@ -0,0 +1,110 @@
import chokidar, { FSWatcher } from "chokidar";
import { Notification } from "electron";
import log from "electron-log/main";
import path from "path";
import errorTypeCheck from "../../util/errorTypeCheck";
import store from "../store/store";
var watcher: FSWatcher;
async function StartWatcher(): Promise<boolean> {
const filePaths = store.get("settings.filepaths") || [];
log.info("Use polling? ", store.get("settings.polling").enabled);
if (filePaths.length === 0) {
new Notification({
//TODO: Add Translations
title: "Watcher cannot start",
body: "Please set the appropriate file paths and try again.",
}).show();
log.warn("Cannot start watcher. No file paths set.");
return false;
}
if (watcher) {
try {
log.info("Trying to close watcher - it already existed.");
await watcher.close();
log.info("Watcher closed successfully!");
} catch (error) {
log.error("Error trying to close Watcher.", error);
}
}
watcher = chokidar.watch(filePaths, {
ignored: (filepath, stats) => {
const p = path.parse(filepath);
return !stats?.isFile() && p.ext !== "" && p.ext.toUpperCase() !== ".ENV";
},
usePolling: store.get("settings.polling").enabled || false,
interval: store.get("settings.polling").pollingInterval || 1000,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
pollInterval: 500,
stabilityThreshold: 2000,
},
});
watcher
.on("add", async function (path) {
console.log("File", path, "has been added");
HandleNewFile(path);
})
// .on("addDir", function (path) {
// console.log("Directory", path, "has been added");
// })
.on("change", async function (path) {
console.log("File", path, "has been changed");
HandleNewFile(path);
})
// .on("unlink", function (path) {
// console.log("File", path, "has been removed");
// })
// .on("unlinkDir", function (path) {
// console.log("Directory", path, "has been removed");
// })
.on("error", function (error) {
log.error("Error in Watcher", errorTypeCheck(error));
})
.on("ready", onWatcherReady)
.on("raw", function (event, path, details) {
// This event should be triggered everytime something happens.
// console.log("Raw event info:", event, path, details);
});
return true;
}
function onWatcherReady() {
log.info("Watcher ready!");
// const b = BrowserWindow.getAllWindows()[0];
// b.webContents.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
new Notification({
title: "Watcher Started",
body: "Newly exported estimates will be automatically uploaded.",
}).show();
log.info("Confirmed watched paths:", watcher.getWatched());
}
async function StopWatcher(): Promise<boolean> {
if (watcher) {
await watcher.close();
log.info("Watcher stopped.");
new Notification({
title: "RPS Watcher Stopped",
body: "Estimates will not be automatically uploaded.",
}).show();
return true;
}
return false;
}
async function HandleNewFile(path) {
//await ImportJob(path);
log.log("Received a new file", path);
}
export { StartWatcher, StopWatcher, watcher };