How to make Gulp.src crash if a file is missing?

Our gulp assembly takes a bunch of libraries installed with bower, and then combines them with all the code that we distribute in several directories. Here's what it looks like:

var jsFiles = [ sourcePath + '/config/config.js', sourcePath + '/vendor/jquery/dist/jquery.js', sourcePath + '/vendor/js-cookie/src/js.cookie.js', sourcePath + '/vendor/modernizr/modernizr.js', sourcePath + '/vendor/lodash/lodash.js', sourcePath + '/vendor/picturefill/dist/picturefill.min.js', sourcePath + '/templates/**/*.js', sourcePath + '/pages/**/*.js' ], gulp.task('build:js', ['jscs'], function() { return gulp.src(jsFiles) .pipe(concat('scripts.js')) .pipe(gulpif(isProd, uglify())) .pipe(gulp.dest(outputPath + '/webresources/js')); }); 

Our problem is that whenever someone adds new libraries, other developers will encounter problems if they do not run bower install to get new components. scripts.js is created without them, because it will not mind that one of the globes is returned empty, even if it is a named file.

How can this be solved? Is there a way to throw an error if glob returns null results?

+10
javascript bower gulp
source share
3 answers

Since there was no ready-made solution for this, I wrote a module that meets our needs.

The files-exist module allows you to check whether all files are present in the array, if there is no error, if they are absent. It returns an identical array when it succeeds, so it is easy to undo it.

  var jsFiles = [ sourcePath + '/config/config.js', sourcePath + '/vendor/jquery/dist/jquery.js', sourcePath + '/vendor/js-cookie/src/js.cookie.js', sourcePath + '/vendor/modernizr/modernizr.js', sourcePath + '/vendor/lodash/lodash.js', sourcePath + '/vendor/picturefill/dist/picturefill.min.js', sourcePath + '/templates/**/*.js', sourcePath + '/pages/**/*.js' ], filesExist = require('files-exist'), gulp.task('build:js', ['jscs'], function() { return gulp.src(filesExist(jsFiles)) // Throws error if a file is missing .pipe(concat('scripts.js')) .pipe(gulpif(isProd, uglify())) .pipe(gulp.dest(outputPath + '/webresources/js')); }); 
+12
source share

I used this IsThere package

https://www.npmjs.com/package/is-there

ex.

 PATHS: javascriptCopyNodeModules: # - "node_modules/name.min.js" import IsThere from 'is-there'; function javascriptCopyNodeModules() { if(IsThere(PATHS.javascriptCopyNodeModules)){ return gulp.src(PATHS.javascriptCopyNodeModules) .pipe(gulp.dest(PATHS.dist + '/assets/js/node_modules')); }else{ return gulp.src('.'); } } 

since PATHS.javascriptCopyNodeModules is empty (#) returns nothing.

0
source share

WAYS: javascriptCopyNodeModules: # - "node_modules / name.min.js"

import IsThere from is;

function javascriptCopyNodeModules () {if (IsThere (PATHS.javascriptCopyNodeModules)) {return gulp.src (PATHS.javascriptCopyNodeModules) .pipe (gulp.dest (PATHS.dist + '/ assets / js / node_modules')); }}

0
source share

All Articles