文件路径在gulp src中有效吗

文件路径在gulp src中有效吗,gulp,task,Gulp,Task,我正在寻找解决方案,这将帮助我找到是文件路径有效。如果文件路径无效,则显示一些错误 gulp.task("scripts-libraries", ["googlecharts"], function () { var scrlibpaths = [ "./node_modules/jquery/dist/jquery.min.js", "./node_modules/bootstrap/dist/js/bootstrap.min.js",

我正在寻找解决方案,这将帮助我找到是文件路径有效。如果文件路径无效,则显示一些错误

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"));
});
gulp.task(“脚本库”[“谷歌图表”],函数(){
变量SCRLBPATHs=[
“/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\u 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”
];
对于(变量i=0;i
那么我该如何使其工作呢?

您可以使用检查传递到
gulp.src()
的路径/全局是否引用现有文件。Gulp本身通过内部使用
glob
,因此这应该是最可靠的选择

这是一个使用
glob
的函数,您可以或多或少地使用它来替代常规的
gulp.src()

然后您可以这样使用它:

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

如果
srclibpaths
中的任何路径/全局不存在,将记录一条警告,并且流将为空(意味着根本不会处理任何文件)。

因为
gulp
与任何其他
节点
脚本一样,您可以使用它来检查文件是否存在(我假设您可能希望是同步的)


这就是我需要的。这是解决我问题的正确方法。
return gulpSrc(scrlibpaths)
  .pipe(plumber())
  .pipe(concat("bundle.libraries.js"))
  .pipe(gulp.dest(config.path.dist + "/js"));
var fs = require('fs');
scrlibpaths.map(function(path) {
    try {
        fs.accessSync(path);
    } catch (e) {
        console.log("There is no " + path + " file on your machine");
    }
});