40 lines
No EOL
1.2 KiB
JavaScript
40 lines
No EOL
1.2 KiB
JavaScript
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;
|
|
}
|
|
watcher.on(constants.FS_EVENT_ADD, (file, stats) => {
|
|
queue.add(constants.FS_EVENT_ADD, file, stats);
|
|
});
|
|
watcher.on(constants.FS_EVENT_UNLINK, (file, stats) => {
|
|
queue.add(constants.FS_EVENT_UNLINK, file, stats);
|
|
});
|
|
watcher.on(constants.FS_EVENT_CHANGE, (file, stats) => {
|
|
queue.add(constants.FS_EVENT_CHANGE, file, stats);
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = Watcher; |