Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
feat(transform): also pass context as an arg
Browse files Browse the repository at this point in the history
Also indicate `transform` is for modifying local commits, and `finalizeContext` is for modifying context lastly.
  • Loading branch information
stevemao committed Apr 17, 2016
1 parent bf88535 commit 9bd984c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ If this value is a `string`, it checks the existence of the field. Set to other

Type: `function` Default: pass through

Last chance to modify your context before generating a changelog.

###### finalizeContext(context, options, commits, keyCommit)

####### context
Expand Down Expand Up @@ -293,13 +295,13 @@ It is possible to customize this the changelog to suit your needs. Templates are

### upstream

Variables in upstream are commit specific and should be used per commit. Eg: *commit date* and *commit username*. You can think of them as "local" or "isolate" variables. A "raw" commit message (original commit poured from upstream) is attached to `commit`.
Variables in upstream are commit specific and should be used per commit. Eg: *commit date* and *commit username*. You can think of them as "local" or "isolate" variables. A "raw" commit message (original commit poured from upstream) is attached to `commit`. `transform` can be used to modify a commit.

### context

context should be module specific and can be used across the whole log. Thus these variables should not be related to any single commit and should be generic information of the module or all commits. Eg: *repository url* and *author names*, etc. You can think of them as "global" or "root" variables.

Basically you can make your own templates and define all your template context. Extra context are based on commits from upstream and `options`. For more details, please checkout [handlebars](http://handlebarsjs.com) and the source code of this module.
Basically you can make your own templates and define all your template context. Extra context are based on commits from upstream and `options`. For more details, please checkout [handlebars](http://handlebarsjs.com) and the source code of this module. `finalizeContext` can be used at last to modify context before generating a changelog.


## CLI
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function conventionalChangelogWriter(context, options) {
return through.obj(function(chunk, enc, cb) {
try {
var result;
var commit = util.processCommit(chunk, options.transform);
var commit = util.processCommit(chunk, options.transform, context);
var keyCommit = commit || chunk;

// previous blocks of logs
Expand Down
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function getNoteGroups(notes, noteGroupsSort, notesSort) {
return retGroups;
}

function processCommit(chunk, transform) {
function processCommit(chunk, transform, context) {
var commit;

try {
Expand All @@ -106,7 +106,7 @@ function processCommit(chunk, transform) {
commit = _.cloneDeep(chunk);

if (_.isFunction(transform)) {
commit = transform(commit);
commit = transform(commit, context);

if (commit) {
commit.raw = chunk;
Expand Down
50 changes: 37 additions & 13 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,22 @@ describe('conventionalChangelogWriter', function() {
});

describe('transform', function() {
it('should ignore the field if it doesn\'t exist', function(done) {
it('should transform the commit with context', function(done) {
var i = 0;

var upstream = through.obj();
upstream.write({
header: 'bla',
body: null,
footer: null,
notes: []
});
upstream.end();
upstream
.pipe(conventionalChangelogWriter())
.pipe(through(function(chunk, enc, cb) {
expect(chunk.toString()).to.equal('<a name=""></a>\n# (' + today + ')\n\n* bla \n\n\n\n');
getStream()
.pipe(conventionalChangelogWriter({}, {
transform: function(commit, context) {
expect(context).to.eql({
commit: 'commits',
issue: 'issues',
date: today
});

return commit;
}
}))
.pipe(through(function(chunk, enc, cb) {
i++;
cb(null);
}, function() {
Expand Down Expand Up @@ -840,6 +840,30 @@ describe('conventionalChangelogWriter', function() {
});
});

it('should ignore the field if it doesn\'t exist', function(done) {
var i = 0;

var upstream = through.obj();
upstream.write({
header: 'bla',
body: null,
footer: null,
notes: []
});
upstream.end();
upstream
.pipe(conventionalChangelogWriter())
.pipe(through(function(chunk, enc, cb) {
expect(chunk.toString()).to.equal('<a name=""></a>\n# (' + today + ')\n\n* bla \n\n\n\n');

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

it('should sort notes on `text` by default', function(done) {
var upstream = through.obj();
upstream.write({
Expand Down

0 comments on commit 9bd984c

Please sign in to comment.