如何通过 Gulp 任务增加版本号?

How to Increment Version Number via Gulp Task?

我想用另一个字符串替换 javascript 文件 (myConstantsFile.js) 中指示版本号的字符串。因此,例如,我的版本号如下所示:“01.11.15”,在 myConstantsFile.js 中这样写,其他常量为:

.constant('productVersion', '1.11.15'); 

现在,我的任务是这样的:

gulp.task('increment-version', function(){
    gulp.src(['./somedir/myConstantsFile.js'])
        .pipe(replace(/'productVersion', '(.*)'/g, '99.99.99'))
        .pipe(gulp.dest('./somedir/'));
});

如您所见,我使用的是常量,而不是 运行 incrementation code,它看起来像这样:

    var numberString = '0.0.1';
    var versionParts = numberString.split('.');
    var vArray = {
      vMajor : versionParts[0],
      vMinor : versionParts[1],
      vPatch : versionParts[2]
    } 

    vArray.vPatch = parseFloat(vArray.vPatch) + 1;
    var periodString = ".";

    var newVersionNumberString = vArray.vMajor + periodString + 
                                vArray.vMinor+ periodString + 
                                vArray.vPatch; 

我需要:

  1. 一种通过正则表达式通过文件 select 当前版本号的方法。
  2. 要知道我可以将逻辑放在最后一个代码块中的什么位置来递增数字并构建新字符串。

安装gulp-bump

npm install gulp-bump --save-dev

安装yargs

npm install yargs --save-dev

需要gulp-凹凸

var bump = require('gulp-bump');

需要 yargs

var args = require('yargs').argv;

你的 bump 任务

gulp.task('bump', function () {
    /// <summary>
    /// It bumps revisions
    /// Usage:
    /// 1. gulp bump : bumps the package.json and bower.json to the next minor revision.
    ///   i.e. from 0.1.1 to 0.1.2
    /// 2. gulp bump --version 1.1.1 : bumps/sets the package.json and bower.json to the 
    ///    specified revision.
    /// 3. gulp bump --type major       : bumps 1.0.0 
    ///    gulp bump --type minor       : bumps 0.1.0
    ///    gulp bump --type patch       : bumps 0.0.2
    ///    gulp bump --type prerelease  : bumps 0.0.1-2
    /// </summary>

    var type = args.type;
    var version = args.version;
    var options = {};
    if (version) {
        options.version = version;
        msg += ' to ' + version;
    } else {
        options.type = type;
        msg += ' for a ' + type;
    }


    return gulp
        .src(['Path to your package.json', 'path to your bower.json'])
        .pipe(bump(options))
        .pipe(gulp.dest('path to your root directory'));
});

VSO 注意:我相信很多来到这个话题的人都会在寻找上面的答案。下面的代码是编辑存储在 npm/bower 包文件某处的版本号,例如 angular 常量:

gulp.task('increment-version', function(){
    //docString is the file from which you will get your constant string
    var docString = fs.readFileSync('./someFolder/constants.js', 'utf8');

    //The code below gets your semantic v# from docString
    var versionNumPattern=/'someTextPreceedingVNumber', '(.*)'/; //This is just a regEx with a capture group for version number
    var vNumRexEx = new RegExp(versionNumPattern);
    var oldVersionNumber = (vNumRexEx.exec(docString))[1]; //This gets the captured group

    //...Split the version number string into elements so you can bump the one you want
    var versionParts = oldVersionNumber.split('.');
    var vArray = {
        vMajor : versionParts[0],
        vMinor : versionParts[1],
        vPatch : versionParts[2]
    };

    vArray.vPatch = parseFloat(vArray.vPatch) + 1;
    var periodString = ".";

    var newVersionNumber = vArray.vMajor + periodString +
                           vArray.vMinor+ periodString +
                           vArray.vPatch;

    gulp.src(['./someFolder/constants.js'])
        .pipe(replace(/'someTextPreceedingVNumber', '(.*)'/g, newVersionNumber))
        .pipe(gulp.dest('./someFolder/'));
});

我省略了一些把我的常量写成一个漂亮的字符串的庞然大物,但这就是要点并且它有效。

从过去 5 小时开始调查 gulp,因为我有一项任务来解决需求。因此,作为 gulp 的菜鸟,我想出了下面的代码 ,没有正则表达式 。感谢 @VSO@Wilmer Saint 快速入门。可能是一个微小的变化,但这对我有帮助。

gulp.task('version', function(){
  var fs = require('fs');
    //docString is the file from which you will get your constant string
    var docString = fs.readFileSync('app/scripts/version/version.js', 'utf8'); //type of docString i an object here.

    var versionParts = docString.split('.');

    var vArray = {
        vMajor : versionParts[0],
        vMinor : versionParts[1],
        vPatch : versionParts[2]
    };

    vArray.vPatch = parseFloat(vArray.vPatch) + 1;
    var periodString = ".";
    var newVersionNumber = vArray.vMajor + periodString +
                           vArray.vMinor+ periodString +
                           vArray.vPatch;



    require('fs').writeFileSync('app/scripts/version/version.js', newVersionNumber + "'");
        return gulp.src(['app/scripts/version/version.js'])
            .pipe(gulp.dest('app/scripts/version/new_version'));//creates version.js file in the directory
    });

或者 return 代码可以如下覆盖 version.js 文件

中的数字
return gulp.src(['app/scripts/version/version.js'],
                {base: './app/scripts/version/version.js'})
        .pipe((gulp.dest('./app/scripts/version/version.js'))) 

我的version.js只有下面的代码

versionBuild = '1.0.8'

我在我的主要功能中使用了以下内容(加载应用程序时加载)

var versionBuild=parseInt(1000*Math.random());
var random = function(digs){
    var rndn;
    if(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1") {
        rndn = Math.random();
        if(digs != undefined && !isNaN(digs)){
              rndn =  parseInt(Math.pow(10, digs)*rndn)
              }
              return rndn;
    }
    else {
        return versionBuild;
    }
}

你可以使用 gulp-bump,它非常简单和可爱:)

npm install --save gulp-bump
const bump = require('gulp-bump');

gulp.task('bump', async () => {
    gulp.src('./package.json')
    .pipe(bump({key: "version"}))
    .pipe(gulp.dest('./'));
  });

Note: use async before a function. it's a requirement.

gulp.task('bump', function() {
    var vers = JSON.parse(fs.readFileSync(__dirname + '/package.json')).version.split('.');
    var second = parseInt(vers[1]);
    var third  = parseInt(vers[2]);
    var options = { key: 'version' };

    if(third == 9 && second != 9) {
        third = 0;
        options.type = 'minor';
    } else if (second == 9 && third == 9) {
        second = 0;
        options.type = 'major';
    }

    gulp.src(__dirname + '/package.json')
    .pipe(bump(options))
    .pipe(gulp.dest('./'));
});