Search code examples
javascriptbuildgulpbuild-processgulp-folders

Gulp: How to read folder name?


I'm refactoring my Gulp build process so the user does not have to enter V=1.2.88 etc.

Rather I would like the user to just type major gulp build, minor gulp build or patch gulp build. Which would then of course iterate the version number.

For this to work however I need gulp to read the folder name of the last build created:

enter image description here

My Current Gulp task which generates the version number:

var version = '';
var env = process.env.V; // V={version number} ie: V=1.0.1 gulp build

gulp.task('version', function() {
    return printOut(env);
});

function errorlog(err) {
    console.log(err.message);
    this.emit('end');
}

function printOut(ver) {
    gutil.log(gutil.colors.blue.bold('Last build: '+paths.last));
    version = ver;
    if (version === undefined) {
        version = '0.0.0';
    }
    gutil.log(gutil.colors.blue.bold('##################################################'));
    gutil.log(gutil.colors.blue.bold('         Building Dashboard version '+version));
    gutil.log(gutil.colors.green.bold('~~           All change is detectable           ~~'));
    gutil.log(gutil.colors.blue.bold('##################################################'));
}

Anyone know how to accomplish this in Gulp?

This is what I found so far Gulp-folders

So using the Gulp-folders plugin I created the following task which runs first:

    gulp.task('build:getLastBuild', folders(paths.lastBuild, function(folder) {
    console.log( 'Last version number is: '+folder);
    return lastVersion = folder;
    //This will loop over all folders inside pathToFolder main, secondary
    //Return stream so gulp-folders can concatenate all of them
    //so you still can use safely use gulp multitasking
    // return gutil.colors.blue.bold('Last build folder: '+folder);
    // return gulp.src(path.join(paths.lastBuild, folder))
    //     .pipe(console.log(' Getting last version number: '+folder))
    //     .pipe(lastVersion = folder);
}));

Now when I run my Build, check it out below! I'm getting the name of the folder in the console.log, however my process errors out :(

TypeError: e.pipe is not a function

enter image description here


Solution

  • I'don't exactly get the part about the minor / majors, but regarding the list of directories, you could do the following:

    var fs = require('fs'),
        gulp = require('gulp');
    
    gulp.task('default', function() {
        var dirs = fs.readdirSync('./build/assets');
        console.log(dirs);
        // do something with your directories
    })
    
    // and the async version:
    gulp.task('async', function() {
        var dirs = [];
        var print = function(err, files) {
            // do something with your directories
            console.log(files)
        };
    
        fs.readdir('./build/assets', print);
    })