node glob pattern include all js except in certain folders
Asked Answered
B

2

5

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
Bettor answered 12/3, 2015 at 21:9 Comment(0)
B
4

There is an ignore option I glazed over in the readme:

glob('**/*.js', { ignore: '{c,d}/**' }, cb)

This will exclude both c + d folders from the match. More here

Bettor answered 12/3, 2015 at 22:17 Comment(0)
K
8

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.

Kattiekatuscha answered 23/12, 2018 at 10:25 Comment(0)
B
4

There is an ignore option I glazed over in the readme:

glob('**/*.js', { ignore: '{c,d}/**' }, cb)

This will exclude both c + d folders from the match. More here

Bettor answered 12/3, 2015 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.