Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node glob pattern include all js except in certain folders

I have a project that looks like this

ls foo/
- file0.js
- a/file1.js
- b/file2.js
- c/file3.js
- d/file4.js

How do I write a glob pattern to exclude the c & d folder but get all other javascript files? I have looked here for an example, but cannot get anything to work.

I imagine the solution would look similar to this:

glob('+(**/*.js|!(c|d))', function(err, file) {
  return console.log(f);
});

I am wanting back

- file0.js
- a/file1.js
- b/file2.js
like image 851
lfender6445 Avatar asked Jan 09 '23 18:01

lfender6445


1 Answers

For environment where there isn't a second parameter to set the exclude, we can achieve such an exception using the pattern demonstrated in the example bellow:

Pattern

/src/**/!(els)/*.scss

structure

/src/style/kit.scss
/src/style/els/some.scss
/src/style/els/two.scss

result

it will select only /src/style/kit.scss

We can use http://www.globtester.com or https://www.digitalocean.com/community/tools/glob to test quickly online.

enter image description here

update

If we are working with Gulp task runner. Or some other tools or classes that offers a second or multiple parameters for exclusion. Or just multiple globs selectors that support exclusion too. Then We can do as the example bellow for gulp show:

for Gulp

We pass an array instead of just a string (multiple globs selectors, one apply after another to add more files or to exclude)

src(['src/style/**/*.{scss,sass}', '!(src/style/els/**)'])

We can have multiple exclusions

watch(['src/style/**/*.{scss,sass}', '!(src/style/els/**)', '!(src/style/_somefileToExclude.scss)'])

In gulp you can use that, with any method that support an array as globs selectors. src and watch are what i used that for.

Note: If you want to exclude a folder and all it's sub folders we use ** as above and not **/* which will not work. If you need some specific files types (extension) then you can use **/*.scss for example.

like image 168
Mohamed Allal Avatar answered Mar 07 '23 08:03

Mohamed Allal