yuanchuan / node-watch

A wrapper and enhancements for fs.watch
https://npm.im/node-watch
MIT License
339 stars 44 forks source link

Add ignore option #96

Closed yuanchuan closed 3 years ago

yuanchuan commented 4 years ago

This might be helpful to set with existing ignore files like .gitignore.

Usage

let options = {
  recursive: true,
  ignore: ['../.gitignore']
}

watch('.', options, console.log)
wmertens commented 4 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

Krinkle commented 3 years ago

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.

wmertens commented 3 years ago

@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.

yuanchuan commented 3 years ago

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.

wmertens commented 3 years ago

@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...

yuanchuan commented 3 years ago

@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.

yuanchuan commented 3 years ago

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);