I’ve been using the auto generated build numbers paired with Stefan Bauer: How to version new SharePoint Framework projects and it’s been working great for myself. There is however, one minor issue. After running ‘npm version major/minor/patch’ it will also run the build/bundle that automatically increments the revision numbers. This is an unwanted side effect.

Running ‘npm version major/minor/patch’ requires a clean tree, but the build/bundle modifies the package-solution.json file causing a problem. I’ve updated the build/bundle code to accept a parameter that will NOT automatically increment the revision number. Ultimately I’m fixing a problem that I caused.

Let’s take a see an example.

First thing that you need is an extra npm package called ‘gulp-util’

npm install --save-dev gulp-util

Here are the gulpfile.js enhancements again, specifically gutil & fs

const gulp = require('gulp');
const build = require('@microsoft/sp-build-web');
const gutil = require('gulp-util');
const fs = require('fs');

Next add the gulp task to bump the revision. You can see that this task is looking for an argument called ‘–no-revision’. If  ‘–no-revision’ is specified in the build or bundle command it will noop on the bumpRevision task.

var getJson = function (file) {
  return JSON.parse(fs.readFileSync(file, 'utf8'));
};

let bumpRevisionSubTask = build.subTask('bump-revision-subtask', function(gulp, buildOptions, done) {
  var skipBumpRevision = buildOptions.args["revision"] === false;
  if(!skipBumpRevision) {
    var pkgSolution = getJson('./config/package-solution.json');
    var oldVersionNumber = String(pkgSolution.solution.version);
    gutil.log('Old Version: ' + oldVersionNumber);
    var oldBuildNumber = parseInt(oldVersionNumber.split('.')[3]);
    gutil.log('Old Build Number: ' + oldBuildNumber);
    var newBuildNumber = oldBuildNumber+1;
    gutil.log('New Build Number: ' + newBuildNumber);
    var newVersionNumber = oldVersionNumber.substring(0, String(oldVersionNumber).length - String(oldBuildNumber).length) + String(newBuildNumber);
    gutil.log('New Version: ' + newVersionNumber);
    pkgSolution.solution.version = newVersionNumber;
    fs.writeFile('./config/package-solution.json', JSON.stringify(pkgSolution, null, 4));
  }
  return gulp.src('./config/package-solution.json')
  .pipe(skipBumpRevision ? gutil.noop() : gulp.dest('./config'))
});

let bumpRevisionTask = build.task('bump-revision', bumpRevisionSubTask);

Lastly register the bumpRevisionTask with the build process.

build.rig.addPreBuildTask(bumpRevisionTask);

Again … all this is above the following

build.initialize(gulp);

Now in order to skip the bumpRevisionTask you can call build/bundle like this:

gulp build --ship --no-revision

gulp bundle --ship --no-revision

That’s it… add this and you will get auto incrementing revision number w/ the option to NOT increment the build number.

 

Why is that so great? Because, if you pair this with Stefan Bauer’s npm versioning post it will run when you execute the ‘npm version major/minor/patch’ command causing a version like v1.0.3.1 … instead of v1.0.3.0

 

I’ll leave you with an example of my build commands for new versions that I’m releasing to dev/staging/prod.

build-patch.cmd

cls

call gulp clean

call gulp build --ship --no-revision

call gulp bundle --ship --no-revision

call npm version patch

call gulp package-solution --ship

call explorer .\sharepoint\solution\

build-minor.cmd

cls

call gulp clean

call gulp build --ship --no-revision

call gulp bundle --ship --no-revision

call npm version minor

call gulp package-solution --ship

call explorer .\sharepoint\solution\

build-major.cmd

cls

call gulp clean

call gulp build --ship --no-revision

call gulp bundle --ship --no-revision

call npm version minor

call gulp package-solution --ship

call explorer .\sharepoint\solution\

I recommend reading some of my other posts on this subject

Original Post: SPFx Automatically Generating Revision Numbers

Simple Build Script for the SharePoint Framework

Stefan Bauer: How to version new SharePoint Framework projects

 

Enjoy! let me know how this works for you and your development teams.

About the Author

Developer, Designer, Thinker, Problem Solver, Office Servers and Services MVP, & Collaboration Director @ SoHo Dragon.

View Articles