kannon/classes/Watcher.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-04-14 14:23:41 +02:00
const path = require('path');
const chokidar = require('chokidar');
class Watcher {
constructor() {
this.#initialize();
}
#initialize() {
if (config?.library === undefined) {
throw new Error('library not defined');
}
if (config?.library?.sources === undefined || config?.library?.sources.length === 0) {
throw new Error('no library sources defined');
}
for (let index = 0; index < config.library.sources.length; index++) {
const directory = path.resolve(config.library.sources[index]);
logger.debug('watching directory \'' + directory + '\'...');
this.#handleEvents(chokidar.watch(directory));
}
}
#handleEvents(watcher) {
if (watcher === undefined) {
return;
}
2022-04-21 13:43:33 +02:00
watcher.on(constants.FS_EVENT_ADD, (file, stats) => {
queue.add(constants.FS_EVENT_ADD, file, stats);
2022-04-14 14:23:41 +02:00
});
2022-04-21 13:43:33 +02:00
watcher.on(constants.FS_EVENT_UNLINK, (file, stats) => {
queue.add(constants.FS_EVENT_UNLINK, file, stats);
2022-04-14 14:23:41 +02:00
});
2022-04-21 13:43:33 +02:00
watcher.on(constants.FS_EVENT_CHANGE, (file, stats) => {
queue.add(constants.FS_EVENT_CHANGE, file, stats);
2022-04-14 14:23:41 +02:00
});
}
}
2022-04-21 13:43:33 +02:00
module.exports = Watcher;