Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Chokidar to watch for specific file extension

I am looking to watch a folder using the nodejs Chokidar I only want to monitor for additions, deletes of xml files. I am new to Chokidar and can't figure it out. I tried setting the Chokidar ignore to match all strings that end in .xml but it looks like Chokidar ignore accepts negative regex's

Even the below example doesn't work

watcher = chokidar.watch(watcherFolder, { 
    ignored: /[^l]$,
    persistent: true,
    ignoreInitial: true,
    alwaysState: true}
);

Is there a way to do this or do I have to add the filter to the callback function?

watcher.on('add', function(path) {
    if (!/\.xml$/i.test(path)) { return; }
    console.log('chokidar: add: ' + path);
});
watcher.on('unlink', function(path) {
    if (!/\.xml$/i.test(path)) { return; }
    console.log('chokidar: unlink: ' + path);
});

watcher.on('change', function(path) {
    if (!/\.xml$/i.test(path)) { return; }
    console.log('chokidar: change: ' + path);
});
like image 379
Trevor Avatar asked Nov 07 '16 15:11

Trevor


1 Answers

chokidar accepts a glob pattern as first argument.
You can use it match your XML files.

chokidar.watch("some/directory/**/*.xml", config)
like image 58
Florent Avatar answered Oct 22 '22 04:10

Florent