Skip to content

Commit

Permalink
feat(exclude): add -x, --exclude option
Browse files Browse the repository at this point in the history
  • Loading branch information
tvardy committed Jun 4, 2017
1 parent 21a8003 commit fc31e9a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/cli.js
Expand Up @@ -4,11 +4,16 @@ var CLI = require('commander');

var Package = require('../package');

function list (val) {
return val.split(',');
}

module.exports = CLI
.description('Generate a changelog from git commits.')
.version(Package.version)
.option('-p, --patch', 'create a patch changelog')
.option('-m, --minor', 'create a minor changelog')
.option('-M, --major', 'create a major changelog')
.option('-x, --exclude', 'exclude selected commit types (comma separated)', list)
.option('-f, --file [file]', 'file to write to, defaults to ./CHANGELOG.md, use - for stdout', './CHANGELOG.md')
.option('-u, --repo-url [url]', 'specify the repo URL for commit links, defaults to checking the package.json');
9 changes: 7 additions & 2 deletions lib/git.js
Expand Up @@ -9,9 +9,11 @@ var FORMAT = '%H%n%s%n%b%n' + SEPARATOR;

/**
* Get all commits from the last tag (or the first commit if no tags).
* @param {Object} options - calculation options
* @returns {Promise<Array<Object>>} array of parsed commit objects
*/
exports.getCommits = function () {
exports.getCommits = function (options) {
options = options || {};
return CP.execAsync('git describe --tags --abbrev=0')
.catch(function () {
return '';
Expand Down Expand Up @@ -53,6 +55,9 @@ exports.getCommits = function () {
return commit;
})
.filter(function (commit) {
return commit !== null;
if (!commit) {
return false;
}
return options.exclude ? (options.exclude.indexOf(commit.type) !== -1) : true;
});
};
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -13,13 +13,14 @@ var Writer = require('./writer');
* @param {Boolean} options.minor - whether it should be a minor changelog
* @param {Boolean} options.major - whether it should be a major changelog
* @param {String} options.repoUrl - repo URL that will be used when linking commits
* @param {Array} options.exclude - exclude listed commit types (e.g. ['chore', 'style', 'refactor'])
* @returns {Promise<String>} the \n separated changelog string
*/
exports.generate = function (options) {
return Bluebird.all([
Package.extractRepoUrl(),
Package.calculateNewVersion(options),
Git.getCommits()
Git.getCommits(options)
])
.spread(function (repoUrl, version, commits) {
options.repoUrl = options.repoUrl || repoUrl;
Expand Down
12 changes: 12 additions & 0 deletions test/data/git/valid-commits.txt
Expand Up @@ -6,3 +6,15 @@ a2c164a718e45d7d3c5e37eec2008a3ebf371e93
feat(testing): did some testing

===END===
9bbdcf32e1c46176c4cbdcef329ada45fed0bf5a
chore(release): 1.2.3
New release
===END===
e45d7d3c5e37eec2008a3ebf371e93a2c164a718
style(all): reviewed semicolons usage

===END===
e45d7d3c5e37eec2008a3ebf371e93a2c164a718
test: add test coverage

===END===
14 changes: 13 additions & 1 deletion test/git.test.js
Expand Up @@ -63,7 +63,7 @@ describe('git', function () {

return Git.getCommits()
.then(function (commits) {
Expect(commits).to.have.length(1);
Expect(commits).to.have.length(4);
Expect(commits[0]).to.have.property('type');
Expect(commits[0]).to.have.property('category');
Expect(commits[0]).to.have.property('subject');
Expand All @@ -83,6 +83,18 @@ describe('git', function () {
});
});

it('skips any excluded commit types', function () {
Sinon.stub(CP, 'execAsync')
.onFirstCall().returns(Bluebird.resolve('v1.2.3'))
.onSecondCall().returns(Bluebird.resolve(VALID_COMMITS));

return Git.getCommits({ exclude: ['chore', 'style'] })
.then(function (commits) {
Expect(commits).to.have.length(2);
CP.execAsync.restore();
});
});

});

});

0 comments on commit fc31e9a

Please sign in to comment.