Use `**` but ignore directories that match the specified name

170 Views Asked by At

I am looking for a glob that matches all descendant directories except those of a specific name. For instance foo/**/*.js matches all *.js files that are descendents of foo/, which is good; the extension of that pattern that I want ignores any descendants that are under a __tests__ directory.

foo/
  file01.js
  __tests__/
    ignore01.js
  bar/
    file02.js
    __tests__/
      ignore02.js
      qux
        ignore04.js
    baz/
      file03.js
      __tests__/
        ignore03.js

What I have tried:

  • foo/**!(__tests__)/*.js
  • foo/!(__tests__)**/*.js
  • foo/**!(__tests__)**/*.js
  • foo/**/!(__tests__)/*.js
  • foo/!(__tests__)/**/*.js
1

There are 1 best solutions below

2
Etheryte On

Not the most elegant solution, but using an expansion should work here:

foo/{,**/!(__tests__)/}*.js

This expands to two conditions which should match your paths, namely

foo/*.js
foo/**/!(__tests__)/*.js

You can read more about globs over here.