-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(changelog-writer): Add commit groups sorting ✨
- Loading branch information
Showing
12 changed files
with
223 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,3 +131,4 @@ $RECYCLE.BIN/ | |
/release-rules.* | ||
/lib | ||
/types.* | ||
/aliases.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {merge, transform, each} from 'lodash'; | ||
import {types} from './types'; | ||
|
||
/** | ||
* @return {Object} Object with each alias as a key and the alias value merge with it's `type` as value. | ||
*/ | ||
export default transform(types, (aliases, value, type) => { | ||
if (value.aliases) { | ||
each(value.aliases, (aliasValue, alias) => { | ||
aliases[alias] = merge({type}, aliasValue); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {findKey, pick} from 'lodash'; | ||
import {types, typesOrder} from '../types'; | ||
import aliases from '../aliases'; | ||
|
||
/** | ||
* Comparison function to sort Commit Groups. | ||
* | ||
* @param {Object} group1 commit group object with a `title` attribute. | ||
* @param {Object} group2 commit group object with a `title` attribute. | ||
* @return {integer} -1 if `group1` should be displayed before `group2`, 1 for the opposite and 0 if they are equals. | ||
*/ | ||
module.exports = (group1, group2) => { | ||
const type1 = typesOrder.indexOf(findKey(types, pick(group1, 'title')) || findKey(aliases, pick(group1, 'title'))); | ||
const type2 = typesOrder.indexOf(findKey(types, pick(group2, 'title')) || findKey(aliases, pick(group2, 'title'))); | ||
|
||
if (type1 !== -1 && type2 === -1) return -1; | ||
if (type1 === -1 && type2 !== -1) return 1; | ||
if (type1 < type2) return -1; | ||
if (type1 > type2) return 1; | ||
if (group1.title < group2.title) return -1; | ||
if (group1.title > group2.title) return 1; | ||
return 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {merge} from 'lodash'; | ||
import conventionalChangelogAngular from 'conventional-changelog-angular'; | ||
const commitGroupsSort = require('./lib/commit-groups-compare'); | ||
const transform = require('./lib/commit-transform'); | ||
|
||
/** | ||
* @type {Promise<Object>} preset with `parserOpts` and `writerOpts`. | ||
*/ | ||
module.exports = conventionalChangelogAngular.then(preset => merge(preset, {writerOpts: {transform}})); | ||
module.exports = conventionalChangelogAngular.then(preset => | ||
merge(preset, {writerOpts: {transform, commitGroupsSort}}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import test from 'ava'; | ||
import emojiRegex from 'emoji-regex/text'; | ||
import {length} from 'stringz'; | ||
import {types, typesOrder} from '../types'; | ||
import aliases from '../aliases'; | ||
|
||
/** | ||
* AVA macro that verifies that each object in `object` has the property `prop` of type `type`. | ||
* | ||
* @method hasProperty | ||
* @param {Object} t AVA assertion librarie | ||
* @param {string} object object to verify | ||
* @param {string} prop property to verify | ||
* @param {string} type type to verify | ||
*/ | ||
function hasProperty(t, object, prop, type) { | ||
for (const obj in object) { | ||
if (Object.prototype.hasOwnProperty.call(object, obj)) { | ||
t.true(Object.prototype.hasOwnProperty.call(object[obj], prop)); | ||
if (type === 'string') { | ||
t.true(typeof object[obj][prop] === type); | ||
} else if (type === 'emoji') { | ||
t.true(emojiRegex({exact: true}).test(object[obj][prop])); | ||
t.is(length(object[obj][prop]), 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
test('Each alias has the property title', hasProperty, aliases, 'title', 'string'); | ||
test('Each type has the property description', hasProperty, aliases, 'description', 'string'); | ||
test('Each type has the property emoji', hasProperty, aliases, 'emoji', 'emoji'); | ||
|
||
test('Each alias`s type property has a value that exists in types', t => { | ||
for (const alias in aliases) { | ||
if (Object.prototype.hasOwnProperty.call(aliases, alias)) { | ||
t.true(Object.prototype.hasOwnProperty.call(types, aliases[alias].type)); | ||
} | ||
} | ||
}); | ||
|
||
test('Each alias exists in typesOrder', t => { | ||
for (const type in types) { | ||
if (Object.prototype.hasOwnProperty.call(types, type)) { | ||
t.true(typesOrder.indexOf(type) !== -1); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import test from 'ava'; | ||
import commitGroupsCompare from './helpers/commit-groups-compare'; | ||
|
||
test('Return ordered commit groups', t => { | ||
const commitGroups = [ | ||
{title: 'Metadata'}, | ||
{title: 'Documentation'}, | ||
{title: 'Bug Fixes'}, | ||
{title: 'Initial'}, | ||
{title: 'Features'}, | ||
]; | ||
const compare = commitGroupsCompare({ | ||
typesOrder: ['feat', 'fix', 'docs', 'initial', 'metadata'], | ||
types: { | ||
feat: {title: 'Features', aliases: {initial: {title: 'Initial'}}}, | ||
fix: {title: 'Bug Fixes', aliases: {metadata: {title: 'Metadata'}}}, | ||
docs: {title: 'Documentation'}, | ||
}, | ||
}); | ||
|
||
t.deepEqual(commitGroups.sort(compare), [ | ||
{title: 'Features'}, | ||
{title: 'Bug Fixes'}, | ||
{title: 'Documentation'}, | ||
{title: 'Initial'}, | ||
{title: 'Metadata'}, | ||
]); | ||
}); | ||
|
||
test('Return alphabeticaly ordered commit groups not in "typesOrder" at the end of the list', t => { | ||
const commitGroups = [ | ||
{title: 'b-Test'}, | ||
{title: 'z-Test'}, | ||
{title: 'Bug Fixes'}, | ||
{title: 'z-Test'}, | ||
{title: 'a-Test'}, | ||
{title: 'Features'}, | ||
]; | ||
const compare = commitGroupsCompare({ | ||
typesOrder: ['feat', 'fix'], | ||
types: { | ||
feat: {title: 'Features'}, | ||
fix: {title: 'Bug Fixes'}, | ||
atest: {title: 'a-Test'}, | ||
ztest: {title: 'z-Test'}, | ||
btest: {title: 'b-Test'}, | ||
}, | ||
}); | ||
|
||
t.deepEqual(commitGroups.sort(compare), [ | ||
{title: 'Features'}, | ||
{title: 'Bug Fixes'}, | ||
{title: 'a-Test'}, | ||
{title: 'b-Test'}, | ||
{title: 'z-Test'}, | ||
{title: 'z-Test'}, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import proxyquire from 'proxyquire'; | ||
|
||
/** | ||
* Return the `commit-groups-compare` function, replacing `types` with parameter. | ||
* | ||
* @method commitGroupsCompare | ||
* @param {Object} types commit types to test with. | ||
* @return {Object} the commit groups compare function. | ||
*/ | ||
export default function commitGroupsCompare(types) { | ||
return proxyquire('../../lib/commit-groups-compare', {'../types': types}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters