Include / exclude globes for gulp.src

I am trying to set up a glob array for my javascript concat build task in gulp. The directory structure looks like this:

├── about
│   └── about.js
├── assets
├── contact
├── core
│   ├── navbar
│   │   ├── navbar.js
│   │   └── navbar.test.js
│   ├── routing.js
│   ├── routing.test.js
│   ├── utils.js
│   └── utils.test.js
├── generated
│   ├── footer.js
│   ├── header.js
│   └── templates.js
├── home
├── app.js
└── config.js

      

The order of the files is important:

  • generated /header.js
  • app.js
  • any of the * .js files other than the ones below
  • generated /templates.js
  • generated /footer.js

I've been madly trying all sorts of wildcard combinations, but flashing isn't great with me.

var inputFiles = [
  'generated/header.js',
  'app.js',
  '!(generated)**/*.js',    // <=---- ???
  'generated/templates.js',
  'generated/footer.js',
  '!**/*.test.js'
];

      

So how do I include all *.js

files except those in a subdirectory?

Thank.

+3


source to share


1 answer


Best I have come up with:

var gulp = require('gulp');
var tap = require('gulp-tap');

gulp.task('default', function() {
    return gulp.src([
        'generated/header.js',
        'app.js',
        '*.js',
        './!(generated)/**/*.js', // <- All subdirs except 'generated'
        'generated/{templates,footer}.js',
        '!**/*.test.js',
        '!node_modules/**'
    ]).pipe(tap(function(file) {
        console.log(file.path);
    }));
});

      

Run:

glob-test gulp
[20:07:51] Using gulpfile ~/Desktop/glob-test/gulpfile.js
[20:07:51] Starting 'default'...
/Users/heikki/Desktop/glob-test/generated/header.js
/Users/heikki/Desktop/glob-test/app.js
/Users/heikki/Desktop/glob-test/config.js
/Users/heikki/Desktop/glob-test/gulpfile.js
/Users/heikki/Desktop/glob-test/about/about.js
/Users/heikki/Desktop/glob-test/core/routing.js
/Users/heikki/Desktop/glob-test/core/utils.js
/Users/heikki/Desktop/glob-test/core/navbar/navbar.js
/Users/heikki/Desktop/glob-test/generated/templates.js
/Users/heikki/Desktop/glob-test/generated/footer.js
[20:07:51] Finished 'default' after 326 ms

      



The main trick is to avoid "!" character at the beginning of a glob when including files.

https://github.com/isaacs/minimatch#comparisons-to-other-fnmatchglob-implementations

"If the pattern starts with!, Then it is negated."

ps. The placement of negative balls is irrelevant. They always move to the end backstage.

+5


source







All Articles