Search code examples
node.jsglobminimatch

What is the glob pattern matching all files that don't start with an underscore ignoring those in directories that start with an underscore?


Given the directory structure:

a/
  b/
    _private/
      notes.txt
    c/
      _3.txt
      1.txt
      2.txt
    d/
      4.txt
    5.txt

How can I write a glob pattern (compatible with npm module glob) which selects the following paths?

a/b/c/1.txt
a/b/c/2.txt
a/b/d/4.txt
a/b/5.txt

Here is what I have tried:

// Find matching file paths inside "a/b/"...
glob("a/b/[!_]**/[!_]*", (err, paths) => {
    console.log(paths);
});

But this only emits:

a/b/c/1.txt
a/b/c/2.txt
a/b/d/4.txt

Solution

  • With some trial and error (and the help of grunt (minimatch/glob) folder exclusion) I found that the following seems to achieve the results that I was looking for:

    // Find matching file paths inside "a/b/"...
    glob("a/b/**/*", {
        ignore: [
            "**/_*",        // Exclude files starting with '_'.
            "**/_*/**"  // Exclude entire directories starting with '_'.
        ]
    }, (err, paths) => {
        console.log(paths);
    });