JAG JAG - 3 months ago 107
Javascript Question

gulp src ignore files from all folder but one

This is my folder structure

views
|
- core
| |
| -core.module.js
| core.controller.js
| some.js
|
- home
|
-home.module.js
home.controller.js
somemore.js
...
...


and in my gulp file I want to include all
js
files from
views
folder recursively and exclude
*.controller.js
from all folders but the
core


i.e select all
js
files from
core
folder and select all but
*.controller.js
from all other folders

I tried like below, but its excluding
*.controller.js
files from
core
folder too.

gulp.src([
//include all from all folders
'views/**/*.js',
//exclude *.controller.js from all folders but core (not working)
'!views/{**, !core}/*.controller.js'
])

Answer

Use the !() glob pattern. From the glob documentation:

!(pattern|pattern|pattern) Matches anything that does not match any of the patterns provided.

So in your case you can do the following:

gulp.src([
  'views/**/*.js', 
  '!views/!(core)/**/*.controller.js'
])
Comments