Skip to content

Commit

Permalink
Adds possibility to override tag name. Fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Jul 24, 2014
1 parent 3f99f43 commit f65cbfd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ Use -m to auto commit and tag. Apply optional message and
use '%s' as placeholder for the updated version. Default
message is 'v%s' where %s is replaced with new version.
--no-prefix (or -n for short) is used when you want to make
a tag without v as prefix. This does not change behaviour of
--tag (or -t for short) allows for overriding the tag name used. This does not
change behaviour of the message, just the tag name. As with -m, all occurances of %s
is replaced with the newly bumped version.
--no-prefix (or -n for short) is a short hand for setting
a tag name without v as prefix. This does not change behaviour of
the message, just the tag name.
--
Ex: "mversion minor -m"
Ex: "mversion minor -m 'Bumped to v%s'"
Ex: "mversion minor -m 'Bumped to v%s' --tag 'v%s-src'"
--
```

### Examples
Expand Down Expand Up @@ -177,23 +182,54 @@ Example:
mversion.update({
version: 'major',
commitMessage: 'Bumps to version %s'
})
});
```

#### `options.tagName : String`
Default: `v%s`

Allows for overriding of tagName. For instance adding a suffix and
changeing tag to be named `v%s-src`.

__Will only take affect if commitMessage is defined.__

Occurances of `%s` in tag name will be replaced with new version number.

Example:
```javascript
mversion.update({
version: 'major',
commitMessage: 'Bumps to version %s',
tagName: 'v%s-src'
});
// Might produce annotated tag named v1.0.0-src
```

#### `options.noPrefix : Boolean`
If true and commitMessage is defined, the annotated tag created
will not have 'v' as prefix.
If true and commit message is defined, the annotated tag created
will not have 'v' as prefix. This is a short hand for defining
setting tag name to `%s`. Do not work if tag name is overriden
(`options.tagName` is defined).

Example:
```javascript
mversion.update({
version: 'major',
commitMessage: 'Bumps to version %s',
noPrefix: true
})
});
// Might produce annotated tag named 1.0.0
```

This would be the same as:
```javascript
mversion.update({
version: 'major',
commitMessage: 'Bumps to version %s',
tagName: '%s'
});
// Might produce annotated tag named 1.0.0
```

### `mversion.isPackageFile(filename) : Boolean`
Checks whether or not the given filename is a valid package file type.
Expand Down
18 changes: 15 additions & 3 deletions bin/version
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var argv = process.argv.slice(2),
thisVersion = require('../package.json').version;

var parsedArguments = require('minimist')(argv, {
'string': ['t', 'tag'],
'boolean': ['n']
});
var defaultMessage = "v%s";
Expand Down Expand Up @@ -57,6 +58,11 @@ function update () {
: parsedArguments.m;
}

// Check for overriding tag name
if (isArgumentPassed('t', 'tag')) {
updateOptions.tagName = parsedArguments.t || parsedArguments.tag;
}

version.update(updateOptions, function (err, data) {
if (err) {
console.error(chalk.red('Failed updating:'));
Expand Down Expand Up @@ -118,13 +124,19 @@ function usage () {
"message is 'v" + chalk.magenta("%s") + "' where " + chalk.magenta("%s") + " is replaced with new version.",
"",

chalk.yellow("--no-prefix") + " (or " + chalk.yellow("-n") + " for short) is used when you want to make",
"a tag without v as prefix. This does not change behaviour of",

chalk.yellow("--tag") + " (or " + chalk.yellow("-t") + " for short) allows for overriding the tag name used. This does not",
"change behaviour of the message, just the tag name. As with " + chalk.yellow("-m") + ", all occurances of " + chalk.magenta("%s"),
"is replaced with the newly bumped version.",
"",

chalk.yellow("--no-prefix") + " (or " + chalk.yellow("-n") + " for short) is a short hand for setting",
"a tag name without v as prefix. This does not change behaviour of",
"the message, just the tag name.",
"",
"--",
"Ex: \"mversion minor -m\"",
"Ex: \"mversion minor -m 'Bumped to v%s'\"",
"Ex: \"mversion minor -m 'Bumped to v%s' --tag 'v%s-src'\"",
"--",
""
].join("\n"))
Expand Down
4 changes: 2 additions & 2 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports.isRepositoryClean = function (callback) {
});
};

module.exports.commit = function (files, message, newVer, noPrefix, callback) {
module.exports.commit = function (files, message, newVer, tagName, callback) {
message = message.replace('%s', newVer).replace('"', '').replace("'", '');

var functionSeries = [
Expand All @@ -37,7 +37,7 @@ module.exports.commit = function (files, message, newVer, noPrefix, callback) {
function (done) {
cp.exec(
[
gitApp, 'tag', '-a', (noPrefix ? '' : 'v') + newVer, '-m', '"' + message + '"'
gitApp, 'tag', '-a', tagName, '-m', '"' + message + '"'
].join(' '),
gitExtra, done
);
Expand Down
24 changes: 22 additions & 2 deletions tests/git_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ describe('git', function () {
return cb(null);
};

git.commit = function (files, message, newVer, noPrefix, callback) {
git.commit = function (files, message, newVer, tagName, callback) {
assert.equal(message, 'Message');
assert.equal(newVer, '1.0.0');
assert.equal(files[0], expectedPath);
assert.equal(noPrefix, false);
assert.equal(tagName, 'v1.0.0');
return callback(null);
};

Expand All @@ -105,6 +105,26 @@ describe('git', function () {
});
});

it('should be able to override tagName', function (done) {
git.isRepositoryClean = function (cb) {
return cb(null);
};

git.commit = function (files, message, newVer, tagName, callback) {
assert.equal(tagName, 'v1.0.0-src');
return callback(null);
};

version.update({
version: '1.0.0',
commitMessage: 'Message',
tagName: 'v%s-src'
}, function (err, data) {
assert.ifError(err);
done();
});
});

it('should get flag defining if v-prefix should be used or not', function (done) {
git.isRepositoryClean = function (cb) {
return cb(null);
Expand Down
12 changes: 8 additions & 4 deletions version.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ exports.update = function (options, callback) {
};
}

if (!options.tagName) {
options.tagName = (options.noPrefix ? '' : 'v') + '%s';
}

var ver = options.version || 'minor';
var noPrefix = !!options.noPrefix;
var commitMessage = options.commitMessage || void 0;
Expand Down Expand Up @@ -131,6 +135,7 @@ exports.update = function (options, callback) {
});

stored.on('end', function () {

var errorMessage = null;
if (errors.length) {
errorMessage = errors.map(function (e) {
Expand All @@ -154,15 +159,14 @@ exports.update = function (options, callback) {
return void 0;
}

git.commit(files, commitMessage, updated.version, noPrefix, function (err) {
var tagName = options.tagName.replace('%s', updated.version).replace('"', '').replace("'", '');
git.commit(files, commitMessage, updated.version, tagName, function (err) {
if (err) {
callback(err, null);
return void 0;
}

ret.message += '\nCommited to git and created tag ';
if (!noPrefix) ret.message += 'v';
ret.message += updated.version;
ret.message += '\nCommited to git and created tag ' + tagName;
callback(null, ret);
});
});
Expand Down

0 comments on commit f65cbfd

Please sign in to comment.