我想myConstantsFile.js用另一个字符串替换指示javascript文件()中版本号的字符串。因此,例如,我的版本号看起来像这样:“ 01.11.15”,myConstantsFile.js 用其他常量这样写:
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/')); });
如您所见,我正在使用一个常量,而不是运行增量代码,它看起来像这样:
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;
我需要:
安装gulp-bump
npm install gulp-bump --save-dev
安装yargs
npm install yargs --save-dev
需要大吃一惊
var bump = require('gulp-bump');
需要yargs
var args = require('yargs').argv;
你的颠簸任务
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软件包文件之外的版本号,例如角度常数:
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/')); });
我省略了一些将其常量写成漂亮字符串的巨型组合,但这就是要点,并且有效。