Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only include if exists

Tags:

gulp

I am looking to include a file in gulp only if it exists, when I am compiling, for development. Currently I have the following:

gulp.task('compile:js:development', function() {
  return gulp.src([
    'src/js/**/*.js',
  ]).pipe(concat('dist.js'))
    .pipe(gulp.dest('compiled/js/'))
});

I need to add another file to this array, but only if that file exists. I have seen gulp-if But I don't think that has the capability I am looking for.

I would also like to warn the developer that this file doesn't exist, when compiling for development in the console.

like image 441
user3379926 Avatar asked Aug 28 '14 14:08

user3379926


1 Answers

Gulp is just a node application, so you can use any node functions in your gulpfile. You can easily check existence of a file using fs.exists()

gulp.task('compile:js:development', function() {
  var fs = require('fs'),
      files = ['src/js/**/*.js'],
      extraFile = 'path/to/other/file';

  if (fs.existsSync(extraFile)) {
    files.push(extraFile);
  } else {
    console.log('FILE DOES NOT EXIST');
  }

  return gulp.src(files)
    .pipe(concat('dist.js'))
    .pipe(gulp.dest('compiled/js/'))
});
like image 90
Brian Glaz Avatar answered Oct 19 '22 18:10

Brian Glaz