Jin Kwon
Jin Kwon

Reputation: 21997

How can I check a src stream is empty with Gulpjs?

I have a task which selects and copies a target environment dependent configuration file.

var environment = process.env.NODE_ENV
        || (gulputil.env.environment || 'production');
process.env.NODE_ENV = environment;

// copies ./src/configs/default-<environment>.json to ./dst/configs/default.json
gulp.task('config-default', function () {
  return gulp.src([paths.src + '/configs/default-' + environment + '.json'])
          .pipe(gulpdebug({title: 'config-default'}))
          .pipe(gulprename('default.json'))
          .pipe(gulp.dest(paths.dst_configs));
});

And it intended to work as following.

$ echo NODE_ENV

$ gulp
environment: production
...
$ gulp --environment staging
environment: staging
...
$ export NODE_ENV=production
$ echo $NODE_ENV
production
$ gulp
environment: production
$

How can I check if someone else specifies a wrong environment variable and consequently there is no file configs/default-<specified>.json?

$ gulp --environment integration
there is no configs/default-integration.json
$

Upvotes: 0

Views: 808

Answers (1)

Sven Schoenung
Sven Schoenung

Reputation: 30574

The easiest way to do this is to just check beforehand if the file exists:

var glob = require('glob');

gulp.task('config-default', function (done) {
  var configFile = paths.src + '/configs/default-' + environment + '.json';

  if (glob.sync(configFile).length == 0) {
    done('there is no ' + configFile);
    return;
  }

  return gulp.src([configFile])
      .pipe(gulpdebug({title: 'config-default'}))
      .pipe(gulprename('default.json'))
      .pipe(gulp.dest(paths.dst_configs));
});

Upvotes: 3

Related Questions