2

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

Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
  • I don't understand the relation between "read the folder name" and your build task. However, I think you should use `yargs` (https://www.npmjs.com/package/yargs), so you could do something like this `gulp --major 2.2.2 build`. If major is the latest version, maybe you just need to use node's `path` module, with a sort function to find the latest version – cl3m Mar 25 '16 at 18:37
  • Well the idea is to ensure that the users does NOT need to know or type in any version number. I want to read the name of the folder, in my example above: `1.2.8` And then if it is `Major` 1+1, or `Minor` 2+1 etc... – Leon Gaban Mar 25 '16 at 18:38
  • What about https://www.npmjs.com/package/semver-utils to check the version and `fs.readdir()` or `fs.readdirSync()` to read the directories? – cl3m Mar 25 '16 at 18:42
  • Ah yeah `fs.readdir` I'll check that out, also checkout the update I posted to my Question. I found a task that can check the folder name, but running into an error. – Leon Gaban Mar 25 '16 at 18:45
  • Regarding your empty action, checkout `gulp-if`: https://github.com/robrich/gulp-if , especially `gulpIgnore` – cl3m Mar 25 '16 at 18:47
  • Still failing :( can you see what I added below my original question? – Leon Gaban Mar 25 '16 at 19:01

2 Answers2

3

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);
})
cl3m
  • 2,791
  • 19
  • 21
  • Ok yeah this is exactly how I solved it :) Checking this, though since I have 2 folders in my `build/assets` I had to put a check in my code below to avoid iterating over the `static` folder. – Leon Gaban Mar 25 '16 at 19:23
1

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

Leon Gaban
  • 36,509
  • 115
  • 332
  • 529