Closed yuanchuan closed 3 years ago
Lol, I'm implementing this ;) I tried implementing the filter optimization as-is, but then I realized via the tests that it would break a lot of current installs, and adding ignore works better. I'll do a pr soon
I think this is quite similar to the filter
option that exists already.
The main difference is that the proposal here would also save (fallback) resources by not watching matching subdirectories in the first place. There is issue https://github.com/yuanchuan/node-watch/issues/93 which proposes to apply that to the filter
option which might be simpler.
For the use case of supporting full gitignore glob patterns, I think it would make sense to document how to use existing libraries for that. E.g. this can be done with one line of code by using picomatch
(or minimatch
) from the filter
option.
@Krinkle actually, I started by implementing filter
but quickly ran into problems with, IIRC, not being able to express ignoring the folder but not subfolders. I don't quite remember but implementing ignore
instead made it backwards-compatible and solved the issue.
Revisiting the problem a few months later, I suppose it's not a good idea to implement the ignore
functionality inside node-watch
since there are so many packages for this already. Add another similar option by name will confuse people to use it too.
@yuanchuan the reason I'm implementing this is to optimize large trees - node-watch shouldn't watch subdirectories that that aren't interesting.
Adding ignore
is the only way I can think of to do this without breaking the API...
@wmertens How about this way? See https://github.com/yuanchuan/node-watch/pull/101
If an explicit skip
string is returned, it will skip the sub-directories.
So the ignore can be implemented like this with the skip
flag added in #101 and the package ignore.
const fs = require('fs');
const watch = require('node-watch');
const ignore = require('ignore');
const matchGitIgnore = (() => {
const ig = ignore().add(fs.readFileSync('./.gitignore', 'utf8'));
return name => ig.ignores(name);
})();
let options = {
recursive: true,
filter(name, skip) {
if (matchGitIgnore(name)) return skip;
return (/\.js$/.test(name));
}
};
watch('./', options, console.log);
Or another simpler way:
const watch = require('node-watch');
const ignore = require('ignore');
function matchIgnoreList(list) {
const ig = ignore().add(list);
return name => ig.ignores(name);
}
let matchIgnore = matchIgnoreList(['node_modules', '.git']);
let options = {
recursive: true,
filter(name, skip) {
if (matchIgnore(name)) return skip;
return true;
}
};
watch('./', options, console.log);
This might be helpful to set with existing ignore files like
.gitignore
.Usage