Last active
September 29, 2017 14:59
-
-
Save rev087/50dc0943750c63ab3553 to your computer and use it in GitHub Desktop.
Gulp task to bump semver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This gulpfile adds three tasks to bump the semver release: bump:major, bump:minor, bump:patch. | |
// First, it checks if the git repository is clean, and fails with an alert otherwise, then: | |
// - Update manifests (in this example, package.json and Info.plist) | |
// - Add manifests to the git stage | |
// - Commit changes in the manifests with the message "Prepare for vX.Y.Z" | |
// - Tag the release | |
// All that is left for the user to do is push the commit/tag to the remote repo with `git push --tags` | |
var gulp = require('gulp'); | |
var semver = require('semver'); | |
var fs = require('fs'); | |
var colors = require('colors'); | |
var plist = require('plist'); | |
var spawn = require('child_process').spawn; | |
function run(cmd, args, callback) { | |
var child = spawn(cmd, args); | |
var buffer = ''; | |
child.stdout.on('data', function (data) { buffer = buffer + data; }); | |
child.stderr.on('data', function (data) { console.error(data.toString()); }); | |
child.on('close', function(code) { | |
if (code !== 0) throw cmd + ' process exited with code ' + code; | |
else callback.apply(this, [buffer]); | |
}); | |
} | |
function bump(release) { | |
// Check if the stage is clean | |
run('git', ['diff', '--staged'], function(res) { | |
if (res.length > 0) { | |
return console.error('\n ' + ' BUMP ERROR '.redBG.bold.black + | |
' Cannot update manifests with a dirty Git stage. \n'.red); | |
} | |
// Bump the version in package.json | |
var pkg = require('./package.json'); | |
var old = pkg.version; | |
pkg.version = 'v' + semver.inc(pkg.version, release); | |
var jsonStr = JSON.stringify(pkg, null, 2); | |
fs.writeFileSync('package.json', jsonStr); | |
// Bump the version in Info.plist | |
var info = plist.parseFileSync('Info.plist'); | |
info.CFBundleShortVersionString = pkg.version; | |
info.CFBundleVersion = pkg.version; | |
var plistStr = plist.build(info).toString(); | |
fs.writeFileSync('Info.plist', plistStr); | |
// Git add | |
run('git', ['add', 'package.json', 'Info.plist'], function() { | |
// Git commit | |
var commitMsg = 'Prepare for ' + pkg.version; | |
run('git', ['commit', '-m', commitMsg], function() { | |
// Git tag | |
run('git', ['tag', pkg.version], function() { | |
// Print a virtual congratulatory pat on the back | |
var msg = ('\n "' + pkg.name + '"').cyan.bold + | |
' bumped from ' + old.green.underline + ' to ' + | |
pkg.version.magenta.underline + '\n'; | |
console.log(msg); | |
}); // tag | |
}); // commit | |
}); // add | |
}); // check stage | |
} | |
gulp.task('bump:major', function() { bump('major'); }); | |
gulp.task('bump:minor', function() { bump('minor'); }); | |
gulp.task('bump:patch', function() { bump('patch'); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gulpfile adds three tasks to bump the semver release: bump:major, bump:minor, bump:patch.
First, it checks if the git repository is clean, and fails with an alert otherwise, then:
All that is left for the user to do is push the commit/tag to the remote repo with
git push --tags