Skip to content

Commit

Permalink
feat(finalizeContext): context.previousVersion defaults to a previo…
Browse files Browse the repository at this point in the history
…us version of generated log
  • Loading branch information
stevemao committed Aug 3, 2015
1 parent c259d01 commit a2df9ca
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -125,6 +125,14 @@ Type: `array`

All git semver tags found in the repository. You can't overwrite this value.

##### previousTag

Type: `string` Default: previous tag or the first commit hash if no previous tag.

##### currentTag

Type: `string` Default: current tag or `'v'` + version if no current tag.

#### gitRawCommitsOpts

See the [git-raw-commits](https://github.com/stevemao/git-raw-commits) docs. There are some defaults:
Expand Down
28 changes: 25 additions & 3 deletions index.js
Expand Up @@ -122,7 +122,7 @@ function conventinalChangelog(options, context, gitRawCommitsOpts, parserOpts, w
}
}

context.gitSemverTags = tagsObj.value;
var gitSemverTags = context.gitSemverTags = tagsObj.value;
if (tagsObj.state === 'fulfilled') {
tag = tagsObj.value[options.releaseCount - 1];
}
Expand Down Expand Up @@ -174,8 +174,30 @@ function conventinalChangelog(options, context, gitRawCommitsOpts, parserOpts, w
},
parserOpts);

writerOpts = _.assign(
preset.writerOpts || {}, {
writerOpts = _.assign({
finalizeContext: function(context, writerOpts, commits, keyCommit) {
if ((!context.currentTag || !context.previousTag) && keyCommit) {
var match = /tag:\s*(.+?)[,\)]/gi.exec(keyCommit.gitTags);
var currentTag = context.currentTag = context.currentTag || match ? match[1] : null;
var index = gitSemverTags.indexOf(currentTag);
var previousTag = context.previousTag = gitSemverTags[index + 1];

if (!previousTag) {
if (options.append) {
context.previousTag = context.previousTag || commits[0].hash;
} else {
context.previousTag = context.previousTag || commits[commits.length - 1].hash;
}
}
} else {
context.previousTag = context.previousTag || gitSemverTags[0];
context.currentTag = context.currentTag || 'v' + context.version;
}

return context;
}
},
preset.writerOpts, {
reverse: options.append
},
writerOpts
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -44,6 +44,7 @@
"chai": "^3.0.0",
"concat-stream": "^1.4.10",
"coveralls": "^2.11.2",
"git-tails": "^1.0.0",
"istanbul": "^0.3.15",
"jscs": "^2.0.0",
"jshint": "^2.8.0",
Expand Down
67 changes: 67 additions & 0 deletions test/test.js
@@ -1,6 +1,7 @@
'use strict';
var conventionalChangelog = require('../');
var expect = require('chai').expect;
var gitTails = require('git-tails');
var shell = require('shelljs');
var through = require('through2');
var writeFileSync = require('fs').writeFileSync;
Expand Down Expand Up @@ -331,7 +332,73 @@ describe('conventionalChangelog', function() {
expect(i).to.equal(3);
done();
}));
});

describe('finalizeContext', function() {
var tail;

before(function(done) {
shell.exec('git tag -d v0.1.0');
gitTails(function(err, data) {
tail = data[data.length - 1].substring(0, 7);
done();
});
});

it('should make `context.previousTag` default to a previous version of generated log (prepend)', function(done) {
var i = 0;

conventionalChangelog({
releaseCount: 0
}, {
version: '3.0.0'
}, {}, {}, {
mainTemplate: '{{previousTag}}...{{currentTag}}'
})
.pipe(through(function(chunk, enc, cb) {
chunk = chunk.toString();

if (i === 0) {
expect(chunk).to.equal('v2.0.0...v3.0.0');
} else if (i === 1) {
expect(chunk).to.equal(tail + '...v2.0.0');
}

i++;
cb();
}, function() {
expect(i).to.equal(2);
done();
}));
});

it('should make `context.previousTag` default to a previous version of generated log (append)', function(done) {
var i = 0;

conventionalChangelog({
releaseCount: 0,
append: true
}, {
version: '3.0.0'
}, {}, {}, {
mainTemplate: '{{previousTag}}...{{currentTag}}'
})
.pipe(through(function(chunk, enc, cb) {
chunk = chunk.toString();

if (i === 0) {
expect(chunk).to.equal(tail + '...v2.0.0');
} else if (i === 1) {
expect(chunk).to.equal('v2.0.0...v3.0.0');
}

i++;
cb();
}, function() {
expect(i).to.equal(2);
done();
}));
});
});

it('should warn if preset is not found', function(done) {
Expand Down

0 comments on commit a2df9ca

Please sign in to comment.