2016-03-08 28 views
0

我有一個任務選擇並複製一個目標環境相關的配置文件。我怎樣才能檢查一個src流與Gulpjs是空的?

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

它打算如下工作。

$ echo NODE_ENV 

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

如何檢查是否有其他人指定了一個錯誤的環境變量,因此沒有文件configs/default-<specified>.json

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

回答

2

做到這一點,最簡單的方法是隻事先檢查文件是否存在:

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