Files
imexrps/electron/file-watcher/file-watcher.js

141 lines
4.2 KiB
JavaScript

const chokidar = require("chokidar");
const ipcTypes = require("../../src/ipc.types");
const path = require("path");
const { DecodeEstimate } = require("../decoder/decoder");
const { BrowserWindow } = require("electron");
const { store } = require("../electron-store");
const {
NewNotification,
} = require("../notification-wrapper/notification-wrapper");
const log = require("electron-log");
var watcher;
async function StartWatcher() {
const filePaths = store.get("filePaths") || [];
log.info("Use polling? ", store.get("polling").enabled);
if (filePaths.length === 0) {
NewNotification({
title: "RPS Watcher cannot start",
body: "Please set the appropriate file paths and try again.",
}).show();
log.warn("Cannot start watcher. No file paths set.");
return [];
}
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: (fp, stats) => {
const p = path.parse(fp);
log.log(
"Checking if should ignore.",
fp,
p,
p.ext !== "" && p.ext !== ".ENV" && p.ext !== ".env"
);
// prettier-ignore
// const ignore = RegExp("^.*(?<!\.env)(?<!\.ENV)$").test(fp);
//console.log("StartWatcher ->", fp, "Ignore?", ignore);
return (p.ext !== "" && p.ext !== ".ENV" && p.ext !== ".env" );
},
usePolling: store.get("polling").enabled || false,
interval: store.get("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) {
console.log("Error happened", error);
const b = BrowserWindow.getFocusedWindow();
b.webContents.send(ipcTypes.fileWatcher.toRenderer.error, 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 filePaths;
}
function onWatcherReady() {
console.log("Ready!");
const b = BrowserWindow.getAllWindows()[0];
b.webContents.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
NewNotification({
title: "RPS Watcher Started",
body: "Newly exported estimates will be automatically uploaded.",
}).show();
console.log("Confirmed watched paths:", watcher.getWatched());
}
async function StopWatcher() {
await watcher.close();
log.info("Watcher stopped.");
const b = BrowserWindow.getAllWindows()[0];
b.webContents.send(ipcTypes.default.fileWatcher.toRenderer.stopSuccess);
NewNotification({
title: "RPS Watcher Stopped",
body: "Estimates will not be automatically uploaded.",
}).show();
}
exports.StartWatcher = StartWatcher;
exports.StopWatcher = StopWatcher;
exports.watcher = watcher;
async function HandleNewFile(path) {
const b = BrowserWindow.getAllWindows()[0];
b.webContents.send(ipcTypes.default.estimate.toRenderer.estimateDecodeStart);
const newJob = await DecodeEstimate(path);
if (newJob && !newJob.ERROR) {
b.webContents.send(
ipcTypes.default.estimate.toRenderer.estimateDecodeSuccess,
newJob
);
log.info(`Sent job for upload. ${newJob.clm_no}`);
NewNotification({
title: "Job Uploaded",
body: "A new job has been uploaded.",
}).show();
} else {
log.info(`Ignored job. ${newJob.ERROR}`);
NewNotification({
title: "Job Ignored",
body: newJob.ERROR,
}).show();
}
}