I want to watch only for files with a specific extension (in my case .json), so ignored has to ignore everything that is NOT .json.
/^(?!.*\.json$).*$/ (found here on SO)/^(.(?!\.json$))+$/ (found here on SO)Result: chokidar ignored all my test files, including ".json".
My guess: chokidar uses another regex-engine than default javascript, so I made a MWE with pure anymatch, which worked like expected:
var anymatch = require('anymatch');
var matchers = [/^(?!.*\.json$).*$/];
console.log(anymatch(matchers, 'foo.json')); // results in false
console.log(anymatch(matchers, 'foo.js')); // results in true
Non-successfull MWE with chokidar:
var chokidar = require('chokidar');
chokidar.watch('testdir', {
ignored: /^(?!.*\.json$).*$/,
}).on('add', function (filename) {
console.log(filename + ' added');
});
Edit: This MWE actually gets executed and finished immediately without feedback. Maybe that is a hint? That behaviour was not visible in my original application (probably no script end because of my client/server-loops etc.).
Specs:
How about chokidar.watch('testdir/**/*.json', ?
Oh, and the reason your original method is not working is
> /^(?!.*\.json$).*$/.test('testdir')
true
testdir is ignored right off the bat, so chokidar does not traverse into its contents
How do you ignore everything except specific file types that may or may not be in a subdirectory?
What @Jakobud said.
How do you ignore everything except specific file types that may or may not be in a subdirectory?
Does anyone have the answer to @Jakobud question?
@AbdulMuqtadeer @Jakobud @vincenzo
An answer was given here: https://github.com/paulmillr/chokidar/issues/951
But I couldn't get it to work. :(
Most helpful comment
Oh, and the reason your original method is not working is
testdiris ignored right off the bat, so chokidar does not traverse into its contents