Skip to content
This repository has been archived by the owner on Nov 21, 2017. It is now read-only.

Commit

Permalink
feat(verbose): output details of the bump
Browse files Browse the repository at this point in the history
If verbose, reason is also printed.

Fixes #5

BREAKING CHANGE: `whatBump` can return an object. `result` is an `object` instead of a `string`.
  • Loading branch information
stevemao committed Apr 2, 2016
1 parent 67883e0 commit 2311c4a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 50 deletions.
34 changes: 12 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@ conventionalRecommendedBump({
```sh
$ npm install --global conventional-recommended-bump
$ conventional-recommended-bump --help

Get a recommended version bump based on conventional commits

Usage
conventional-recommended-bump

Example
conventional-recommended-bump

Options
-p, --preset Name of the preset you want to use
-h, --header-pattern Regex to match header pattern
-c, --header-correspondence Comma separated parts used to define what capturing group of headerPattern captures what
-r, --reference-actions Comma separated keywords that used to reference issues
-i, --issue-prefixes Comma separated prefixes of an issue
-n, --note-keywords Comma separated keywords for important notes
-f, --field-pattern Regex to match other fields
-v, --verbose Verbose output
```


Expand All @@ -71,7 +53,11 @@ A set of options of a popular project.

Type: `function`

A function that takes parsed commits as argument and returns a number indicating what bump it should be.
A function that takes parsed commits as argument.

Return an object including `level` and `reason`. level is a `number` indicating what bump it should be and `reason` is the reason of such release.

For backward compatibility, it could return a `number` indicating what bump it should be.

###### whatBump(commits)

Expand All @@ -91,13 +77,17 @@ See the [conventional-commits-parser](https://github.com/stevemao/conventional-c

Type: `function`

##### callback(error, releaseAs)
##### callback(error, object)

###### object

object includes what's returned by `whatBump` and

###### releaseAs
####### releaseAs

Type: `string` Possible values: `'major'`, `'minor'` and `'patch'`

The value of what it should release as. If it cannot decide this is an empty string.
The value of what it should release as.


## Related
Expand Down
16 changes: 10 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,30 @@ var cli = meow({
}
});

var options;
var options = {};
var flags = cli.flags;
var preset = flags.preset;

if (preset) {
options = {
preset: preset
};
options.preset = preset;
delete flags.preset;
}

if (flags.verbose) {
options.warn = console.warn.bind(console);
}

conventionalRecommendedBump(options, flags, function(err, releaseAs) {
conventionalRecommendedBump(options, flags, function(err, data) {
if (err) {
console.error(err.toString());
process.exit(1);
}

console.log(releaseAs);
if (data.releaseAs) {
console.log(data.releaseAs);
}

if (flags.verbose && data.reason) {
console.log('Reason: ' + data.reason);
}
});
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ function conventionalRecommendedBump(options, parserOpts, cb) {
options.warn('No commits since last release');
}

var level = whatBump(commits);
var releaseAs = VERSIONS[level];
var result = whatBump(commits);

if (releaseAs) {
cb(null, releaseAs);
} else {
cb(null, '');
if (typeof result === 'number') {
result = {
level: result
};
}

if (result && result.level != null) {
result.releaseAs = VERSIONS[result.level];
} else if (result == null) {
result = {};
}

cb(null, result);
}));
});
}
Expand Down
16 changes: 12 additions & 4 deletions presets/angular.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
var presetOpts = {
whatBump: function(commits) {
var level = 2;
var breakings = 0;
var features = 0;

commits.some(function(commit) {
commits.forEach(function(commit) {
if (commit.notes.length > 0) {
breakings += commit.notes.length;
level = 0;
return true;
} else if (commit.type === 'feat') {
level = 1;
features += 1;
if (level === 2) {
level = 1;
}
}
});

return level;
return {
level: level,
reason: 'There are ' + breakings + ' BREAKING CHANGES and ' + features + ' features'
};
},
parserOpts: {
headerPattern: /^(\w*)(?:\((.*)\))?\: (.*)$/,
Expand Down
44 changes: 37 additions & 7 deletions test/angular.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var child = require('child_process');
var conventionalRecommendedBump = require('../');
var equal = require('assert').strictEqual;
var equal = require('assert').deepStrictEqual;
var shell = require('shelljs');
var writeFileSync = require('fs').writeFileSync;

Expand All @@ -28,7 +28,12 @@ describe('preset', function() {

it('should release as minor', function(done) {
conventionalRecommendedBump(opts, function(err, releaseAs) {
equal(releaseAs, 'minor');
equal(releaseAs, {
level: 1,
reason: 'There are 0 BREAKING CHANGES and 1 features',
releaseAs: 'minor'
});

done();
});
});
Expand All @@ -37,7 +42,12 @@ describe('preset', function() {
conventionalRecommendedBump(opts, {
headerPattern: /^(\w*)\: (.*)$/,
}, function(err, releaseAs) {
equal(releaseAs, 'patch');
equal(releaseAs, {
level: 2,
reason: 'There are 0 BREAKING CHANGES and 0 features',
releaseAs: 'patch'
});

done();
});
});
Expand All @@ -47,7 +57,12 @@ describe('preset', function() {
// fix this until https://github.com/arturadib/shelljs/issues/175 is solved
child.exec('git add --all && git commit -m "feat(): amazing new module" -m "BREAKING CHANGE: Not backward compatible."', function() {
conventionalRecommendedBump(opts, function(err, releaseAs) {
equal(releaseAs, 'major');
equal(releaseAs, {
level: 0,
reason: 'There are 1 BREAKING CHANGES and 1 features',
releaseAs: 'major'
});

done();
});
});
Expand All @@ -58,7 +73,12 @@ describe('preset', function() {
// fix this until https://github.com/arturadib/shelljs/issues/175 is solved
child.exec('git add --all && git commit -m "feat(): another amazing new module" -m "Super backward compatible."', function() {
conventionalRecommendedBump(opts, function(err, releaseAs) {
equal(releaseAs, 'major');
equal(releaseAs, {
level: 0,
reason: 'There are 1 BREAKING CHANGES and 2 features',
releaseAs: 'major'
});

done();
});
});
Expand All @@ -70,7 +90,12 @@ describe('preset', function() {
// fix this until https://github.com/arturadib/shelljs/issues/175 is solved
child.exec('git add --all && git commit -m "revert: feat(): amazing new module" -m "This reverts commit ' + hash.trim() + '."', function() {
conventionalRecommendedBump(opts, function(err, releaseAs) {
equal(releaseAs, 'minor');
equal(releaseAs, {
level: 1,
reason: 'There are 0 BREAKING CHANGES and 2 features',
releaseAs: 'minor'
});

done();
});
});
Expand All @@ -82,7 +107,12 @@ describe('preset', function() {
preset: 'angular',
ignoreReverted: false
}, function(err, releaseAs) {
equal(releaseAs, 'major');
equal(releaseAs, {
level: 0,
reason: 'There are 1 BREAKING CHANGES and 2 features',
releaseAs: 'major'
});

done();
});
});
Expand Down
37 changes: 32 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var assert = require('assert');
var conventionalRecommendedBump = require('../');
var equal = assert.strictEqual;
var equal = require('assert').deepStrictEqual;
var fs = require('fs');
var shell = require('shelljs');

Expand All @@ -25,22 +25,45 @@ describe('conventional-recommended-bump', function() {
});
});

it('should return `""` if no `whatBump` is found', function(done) {
it('should return `{}` if no `whatBump` is found', function(done) {
shell.exec('git add --all && git commit -m"First commit"');

conventionalRecommendedBump({}, function(err, releaseAs) {
equal(releaseAs, '');
equal(releaseAs, {});

done();
});
});

it('should be a mojor bump', function(done) {
it('should return what is returned by `whatBump`', function(done) {
shell.exec('git add --all && git commit -m"First commit"');

conventionalRecommendedBump({
whatBump: function() {
return {
test: 'test'
};
}
}, function(err, releaseAs) {
equal(releaseAs, {
test: 'test'
});

done();
});
});

it('should be a major bump', function(done) {
conventionalRecommendedBump({
whatBump: function() {
return 0;
}
}, function(err, releaseAs) {
equal(releaseAs, 'major');
equal(releaseAs, {
level: 0,
releaseAs: 'major'
});

done();
});
});
Expand All @@ -56,6 +79,10 @@ describe('conventional-recommended-bump', function() {
});
});

it('`warn` is optional', function(done) {
conventionalRecommendedBump({}, done);
});

it('should get the commits from last tag', function(done) {
fs.writeFileSync('test2', '');
shell.exec('git add --all && git commit -m"Second commit"');
Expand Down

0 comments on commit 2311c4a

Please sign in to comment.