Added application settings and basic filewatcher.

This commit is contained in:
Patrick Fic
2020-10-13 12:59:43 -07:00
parent 36b3719df0
commit 6dddd4bdf0
10 changed files with 207 additions and 18 deletions

View File

@@ -0,0 +1,15 @@
const { ipcMain } = require("electron");
const { StartWatcher, StopWatcher } = require("./file-watcher");
const ipcTypes = require("../../src/ipc.types").default;
ipcMain.on(ipcTypes.filewatcher.start, async (event, arg) => {
console.log(ipcTypes.filewatcher.start);
const filePaths = StartWatcher();
event.sender.send(ipcTypes.filewatcher.startSuccess, filePaths);
});
ipcMain.on(ipcTypes.filewatcher.stop, async (event, arg) => {
console.log(ipcTypes.filewatcher.start);
StopWatcher();
event.sender.send(ipcTypes.filewatcher.start, { success: true });
});

View File

@@ -0,0 +1,59 @@
const chokidar = require("chokidar");
const { file } = require("electron-settings");
const settings = require("electron-settings");
var watcher;
function StartWatcher() {
const filePaths = settings.getSync("filePaths") || [];
console.log("StartWatcher -> filePaths", filePaths);
watcher = chokidar.watch(filePaths, {
ignored: /[\/\\]\./,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
pollInterval: 100,
stabilityThreshold: 2000,
},
});
watcher
.on("add", function (path) {
console.log("File", path, "has been added");
})
.on("addDir", function (path) {
console.log("Directory", path, "has been added");
})
.on("change", function (path) {
console.log("File", path, "has been changed");
})
.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);
})
.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.info(
"From here can you check for real changes, the initial scan has been completed."
);
}
async function StopWatcher() {
await watcher.close();
console.log("closed", watcher);
}
exports.StartWatcher = StartWatcher;
exports.StopWatcher = StopWatcher;
exports.watcher = watcher;