2016-07-08 3 views
1

私が見つけるのを助ける解決策を探していますが、有効なファイルパスです。そして、ファイルパスが何らかのエラーを表示するのに有効でない場合。ファイルパスがgulp srcで有効ですか

gulp.task("scripts-libraries", ["googlecharts"], function() { 
    var scrlibpaths = [ 
      "./node_modules/jquery/dist/jquery.min.js", 
      "./node_modules/bootstrap/dist/js/bootstrap.min.js", 
      "./libs/AdminLTE-2.3.0/plugins/slimScroll/jquery.slimscroll.min.js", 
      "./libs/AdminLTE-2.3.0/plugins/fastclick/fastclick.min.js", 
      "./libs/adminLTE-app.js", 
      "./node_modules/moment/min/moment.min.js", 
      "./node_modules/jquery.inputmask/dist/jquery.inputmask.bundle.js", 
      "./node_modules/bootstrap-timepicker/js/bootstrap-timepicker.min.js", 
      "./node_modules/bootstrap-checkbox/dist/js/bootstrap-checkbox.min.js", 
      "./node_modules/bootstrap-daterangepicker/daterangepicker.js", 
      "./node_modules/select2/dist/js/select2.full.min.js", 
      "./node_modules/toastr/build/toastr.min.js", 
      "./node_modules/knockout/build/output/knockout-latest.js", 
      "./node_modules/selectize/dist/js/standalone/selectize.min.js", 
      //"./src/jquery.multiselect.js" 
    ]; 

    for (var i = 0; i < scrlibpaths.length; i++) { 
     if (scrlibpaths[i].pipe(size()) === 0) { 
      console.log("There is no" + scrlibpaths[i] + " file on your machine"); 
      return; 
     } 
    } 

    return gulp.src(scrlibpaths) 
     .pipe(plumber()) 
     .pipe(concat("bundle.libraries.js")) 
     .pipe(gulp.dest(config.path.dist + "/js")); 
}); 

これを動作させるにはどうすればよいですか?

答えて

2

glob moduleを使用して、gulp.src()に渡すパス/グロブが既存のファイルを参照しているかどうかを確認できます。 Gulp自体はglob-stream経由でglobを内部で使用していますので、これは最も信頼性の高いオプションです。

はここglobを使用して、あなたは多かれ少なかれ、ドロップの正規gulp.src()の代わりとして使うことができる機能です。

var glob = require('glob'); 

function gulpSrc(paths) { 
    paths = (paths instanceof Array) ? paths : [paths]; 
    var existingPaths = paths.filter(function(path) { 
    if (glob.sync(path).length === 0) { 
     console.log(path + ' doesnt exist'); 
     return false; 
    } 
    return true; 
    }); 
    return gulp.src((paths.length === existingPaths.length) ? paths : []); 
} 

あなたは、このようにそれを使用することができます:

return gulpSrc(scrlibpaths) 
    .pipe(plumber()) 
    .pipe(concat("bundle.libraries.js")) 
    .pipe(gulp.dest(config.path.dist + "/js")); 

srclibpathsのパス/グロブのいずれかが存在しない場合、警告が記録され、ストリームは空になります(つまり、ファイルがまったく処理されないことを意味します)。

+0

これは私が必要なものです。そして、これは私の問題の解決策でした。 – hongchen

0

gulpは他のnodeスクリプトと同じように、accessSyncを使用してファイルが存在するかどうかを確認できます(おそらく同期していると仮定します)。

var fs = require('fs'); 
scrlibpaths.map(function(path) { 
    try { 
     fs.accessSync(path); 
    } catch (e) { 
     console.log("There is no " + path + " file on your machine"); 
    } 
}); 
関連する問題