mpen
mpen

Reputation: 283043

How to get gulp to error on missing file?

I've got gulpfile.js set up like this:

var scripts = [
    'bower_components/timezone-js/src/date.js',                            
    'bower_components/jquery/jquery.min.js',                               
    'bower_components/jquery-migrate/jquery-migrate.js',                   
    'bower_components/jquery-ui/ui/minified/jquery-ui.min.js',              
    'bower_components/jqueryui-touch-punch/jquery.ui.touch-punch.min.js',  
    ...
];

gulp.task('scripts', function () {
    return gulp.src(scripts, {base: '.'})
        .pipe(plumber(plumberOptions))
        .pipe(sourcemaps.init({
            loadMaps: false,
            debug: debug,
        }))
        ...

i.e., all my script files are exact matches. No globbing.

Every now and then I mess up a file path or the author changes the directory structure. I want to be notified when this happens instead of the script silently being excluded and causing run-time errors.

Is there some way for me to make gulp.src report these kinds of errors?

Upvotes: 8

Views: 3083

Answers (2)

mpen
mpen

Reputation: 283043

Use gulp-expect-file as per this answer.

var coffee = require('gulp-coffee');
var expect = require('gulp-expect-file');

gulp.task('mytask', function() {
  var files = ['idontexist.html'];

  return gulp.src(files)
    .pipe(expect(files))
    .pipe(coffee());
});

(Thanks rve)

Upvotes: 6

mpen
mpen

Reputation: 283043

gulp.src is actually just an alias to vinyl-fs.src which looks like this:

function src(glob, opt) {
  opt = opt || {};
  var pass = through.obj();

  if (!isValidGlob(glob)) {
    throw new Error('Invalid glob argument: ' + glob);
  }
  // return dead stream if empty array
  if (Array.isArray(glob) && glob.length === 0) {
    process.nextTick(pass.end.bind(pass));
    return pass;
  }

  var options = defaults(opt, {
    read: true,
    buffer: true
  });

  var globStream = gs.create(glob, options);

  // when people write to use just pass it through
  var outputStream = globStream
    .pipe(through.obj(createFile))
    .pipe(getStats(options));

  if (options.read !== false) {
    outputStream = outputStream
      .pipe(getContents(options));
  }

  return outputStream.pipe(pass);
}

It in turn uses glob-stream which uses glob. You can probably bypass most of that and use through2 directly to create a pipe from the array files. I haven't figured out how to do this yet.

Upvotes: 0

Related Questions