42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
|
const path = require('path');
|
||
|
const chokidar = require('chokidar');
|
||
|
|
||
|
const { FS_EVENT_ADD: EVENT_ADD, FS_EVENT_UNLINK: EVENT_UNLINK, FS_EVENT_CHANGE: EVENT_CHANGE } = require('../libs/constants.js');
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
watcher.on(EVENT_ADD, (file, stats) => {
|
||
|
queue.add(EVENT_ADD, file, stats);
|
||
|
});
|
||
|
watcher.on(EVENT_UNLINK, (file, stats) => {
|
||
|
queue.add(EVENT_UNLINK, file, stats);
|
||
|
});
|
||
|
watcher.on(EVENT_CHANGE, (file, stats) => {
|
||
|
queue.add(EVENT_CHANGE, file, stats);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Watcher, EVENT_ADD, EVENT_UNLINK;
|