Leon Gaban
Leon Gaban

Reputation: 39018

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

Upvotes: 2

Views: 4024

Answers (2)

Leon Gaban
Leon Gaban

Reputation: 39018

Got it! Though I admit it is a little rough, I googled node readdir and found __dirname console.log(__dirname);

So with that, I created the variables and task below:

var fs   = require('fs'),
    path = require('path');

gulp.task('build:getLastBuild', function() {
    return fs.readdirSync(paths.lastBuild).filter(function(file) {
        console.log(' build:getLastBuild: '+file);
        if (file != 'static') {
            lastVersion = file;
        }
        else {
            console.log('  lastVersion: '+lastVersion);
        }
    });
}); 

So now getting this! Now I have a string that I can manipulate to increase the version number when users run the build process.

enter image description here

Upvotes: 1

cl3m
cl3m

Reputation: 2801

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

Upvotes: 3

Related Questions