hongchen
hongchen

Reputation: 85

Is file path valid in gulp src

I'm looking for solution which will help me to find is file path valid. And if file path not valid to show some error.

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

So how can i make this to work?

Upvotes: 1

Views: 1321

Answers (2)

martin
martin

Reputation: 96969

Since gulp is just like any other node script you can use accessSync to check whether the file exists (I assume you probably want to be synchronous).

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

Upvotes: 0

Sven Schoenung
Sven Schoenung

Reputation: 30574

You can use the glob module to check if the paths/globs you pass to gulp.src() refer to existing files. Gulp itself uses glob internally via glob-stream so this should be the most reliable option.

Here's a function that uses glob and that you can use as a more or less drop-in replacement for the regular 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 : []);
}

You can then use it like this:

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

If any of the paths/globs in srclibpaths doesn't exist a warning is logged and the stream will be empty (meaning no files will be processed at all).

Upvotes: 2

Related Questions