60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
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;
|