diff --git a/index.js b/index.js index 527b92c..9d7776b 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var map = require('map-stream'), gutil = require('gulp-util'), - git = require('gulp-git') + git = require('gulp-git'), + gift = require('gift'); module.exports = function(opts) { if(!opts) opts = {} @@ -16,7 +17,33 @@ module.exports = function(opts) { var json = JSON.parse(file.contents.toString()), tag = opts.prefix+json[opts.key] gutil.log('Tagging as: '+gutil.colors.cyan(tag)) - git.tag(tag, 'tagging as '+tag, opts) + + // gift is a full-fledge git plugin + var APP_DIR = process.env.PWD; + var repo = gift(APP_DIR); + + // Retrieve all the existing tags + repo.tags(function(err, tags) { + // Collect their names + var tagNames = tags.map(function(tag) { + return tag.name; + }); + + // If it does not exist, we can tag safely. + if (tagNames.indexOf(tag) < 0) { + git.tag(tag, 'tagging as '+tag, opts); + + } else { + // Revert the "bump" commit because the command would fail. + gutil.log("Tag " + gutil.colors.cyan(tag) + " exists! Revering commit..."); + repo.reset("HEAD^", function(err) { + if (err) { + return cb(new Error(err)); + } + }); + } + }); + cb(null, file) } diff --git a/package.json b/package.json index 9215b47..a46b492 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-tag-version", - "version": "1.1.0", + "version": "1.1.1", "description": "Tag git repository with current package version", "main": "index.js", "scripts": {