最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make Gulp.src fail if a file is missing? - Stack Overflow

programmeradmin1浏览0评论

Our gulp build takes a bunch of libraries installed with bower, then concatenates them with all the code we have distributed across 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 haven't run bower install to get the new components. The scripts.js gets built without them since it won't mind that one of the globs returns empty, even if it is a named file.

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

Our gulp build takes a bunch of libraries installed with bower, then concatenates them with all the code we have distributed across 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 haven't run bower install to get the new components. The scripts.js gets built without them since it won't mind that one of the globs returns empty, even if it is a named file.

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

Share Improve this question asked Oct 19, 2015 at 7:42 KaivosukeltajaKaivosukeltaja 15.7k5 gold badges43 silver badges71 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 14

Since there didn't seem to be a ready solution for this, I wrote a module to fit our needs.

The files-exist module allows you to check whether all files in an array are present, throwing an error if any are missing. It returns an identical array on success, so it is simple to drop in place.

  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'));
});

I have used this package IsThere

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 (#) return nothing.

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('path_of_file'); } }

You can use gulp-plumber a gulp plugin to get error while building.

Example to illustrate:

Step 1: Install the gulp-plumber:

npm i --save-dev gulp-plumber 

Step 2: Require the gulp plmber module in gulpfile:

...
var plumber = require('gulp-plumber'),
...   

Step 3: Usage

gulp.task('concatFiles', function (done) {
    
    gulp.src(['../a.js', '../b.js', '../c.js'])
        .pipe(plumber())
        .pipe(concat('destination.js'))
        .pipe(plumber.stop())
        .pipe(gulp.dest('dist/js'));

    done(); // a callback required in gulp 4
});
发布评论

评论列表(0)

  1. 暂无评论