Comments (7)
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
from node-watch.
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 #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.
from node-watch.
@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.
from node-watch.
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.
from node-watch.
@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...
from node-watch.
@wmertens How about this way? See #101
If an explicit skip
string is returned, it will skip the sub-directories.
from node-watch.
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);
from node-watch.
Related Issues (20)
- Follow symlink HOT 2
- watch dir bug HOT 2
- Calling close() before the watcher is ready does nothing HOT 2
- Avoid synchronous fs calls HOT 1
- Catch errors when adding watchers for child directories HOT 1
- Catch error when use Node 14.15.3 on Linux SysTem HOT 2
- I got this error at first use lib in nodejs using express HOT 2
- node-watch is not working recursive in Ubuntu under WSL HOT 5
- when watch a dir that mount by nfs, if not work HOT 2
- New ES syntax HOT 2
- Only pass skip on initial recurse
- Travis CI is no longer free, try to switch to GitHub CI
- Fix tests on Windows
- fs.watch() not detecting changes to file from git operation HOT 3
- images update problem HOT 3
- Can TEMP_DIR able to change ?
- Is there a way, or a best practice to scan this large amount of files for processing ?
- multiple events for a file sent over network HOT 2
- High CPU consumption on window service HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-watch.