Search code examples
gulp

Include file if other file doesn't exist in Gulp


I have a product that has a base version, but also customized versions for separate customers. I am using ASP.NET bundleconfigs to include specific files for the customers build, but I am trying to switch to Gulp.

This is my simplified directory structure:

- App
- App / Js
- App / Js / Base
- App / Js / Base / file1.js
- App / Js / Base / file2.js
- App / Js / Base / file3.js
- App / Js / Customer1
- App / Js / Customer1 / file2.js

What I need to do is build a single JS file that contains file1.js from Base, file2.js from the Customer1 directory and file3.js from Base again.

Is this possible with Gulp or should I look in writing a plugin?


Solution

  • You can use gulp-filter to filter out those files in App/Js/Base/ that have a corresponding file in App/Js/Customer1/.

    var gulp = require('gulp');
    var concat = require('gulp-concat');
    var filter = require('gulp-filter');
    var addsrc = require('gulp-add-src');
    var fileExists = require('file-exists');
    var path = require('path');
    
    gulp.task('default', function() {
      return gulp.src('App/Js/Base/*.js')
       .pipe(filter(function(file) {
         return !fileExists.sync('App/Js/Customer1/' + path.basename(file.path));
       }))
       .pipe(addsrc('App/Js/Customer1/*.js'))
       .pipe(concat('all.js'))
       .pipe(gulp.dest('dist'));
    });