From 2311c4a7d7fc58bbea66276b1c55870e2e1e6dda Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Sat, 2 Apr 2016 13:51:25 +1100 Subject: [PATCH] feat(verbose): output details of the bump If verbose, reason is also printed. Fixes #5 BREAKING CHANGE: `whatBump` can return an object. `result` is an `object` instead of a `string`. --- README.md | 34 ++++++++++++---------------------- cli.js | 16 ++++++++++------ index.js | 19 +++++++++++++------ presets/angular.js | 16 ++++++++++++---- test/angular.js | 44 +++++++++++++++++++++++++++++++++++++------- test/test.js | 37 ++++++++++++++++++++++++++++++++----- 6 files changed, 116 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 4a9780c..3b56d67 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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) @@ -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 diff --git a/cli.js b/cli.js index dc81e77..2a22653 100755 --- a/cli.js +++ b/cli.js @@ -34,14 +34,12 @@ 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; } @@ -49,11 +47,17 @@ 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); + } }); diff --git a/index.js b/index.js index e98fc5e..6aaab45 100644 --- a/index.js +++ b/index.js @@ -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); })); }); } diff --git a/presets/angular.js b/presets/angular.js index 6a58e62..e2161e7 100644 --- a/presets/angular.js +++ b/presets/angular.js @@ -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*)(?:\((.*)\))?\: (.*)$/, diff --git a/test/angular.js b/test/angular.js index ba75aab..d60fcff 100644 --- a/test/angular.js +++ b/test/angular.js @@ -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; @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); diff --git a/test/test.js b/test/test.js index 15e188b..83a3901 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); @@ -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(); }); }); @@ -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"');