Added application settings and basic filewatcher.
This commit is contained in:
15
electron/file-watcher/file-watcher-ipc.js
Normal file
15
electron/file-watcher/file-watcher-ipc.js
Normal 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 });
|
||||
});
|
||||
59
electron/file-watcher/file-watcher.js
Normal file
59
electron/file-watcher/file-watcher.js
Normal 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;
|
||||
Reference in New Issue
Block a user