Skip to content

Commit

Permalink
fix(preset): Fix changelog group title order with emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Aug 27, 2017
1 parent 507eece commit 2a07254
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 49 deletions.
16 changes: 7 additions & 9 deletions src/lib/commit-groups-compare.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {findKey, pick} from 'lodash';
import {types, typesOrder} from '../types';
import aliases from '../aliases';
import {typesOrder} from '../types';

/**
* Comparison function to sort Commit Groups.
Expand All @@ -10,13 +8,13 @@ import aliases from '../aliases';
* @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')));
const idx1 = typesOrder.indexOf(group1.commits[0].type);
const idx2 = typesOrder.indexOf(group2.commits[0].type);

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 (idx1 !== -1 && idx2 === -1) return -1;
if (idx1 === -1 && idx2 !== -1) return 1;
if (idx1 < idx2) return -1;
if (idx1 > idx2) return 1;
if (group1.title < group2.title) return -1;
if (group1.title > group2.title) return 1;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/commit-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = (commit, context) => {
}

if (types[commit.type] && (types[commit.type].changelog || (commit.notes && commit.notes.length > 0))) {
commit.type = `${types[commit.type].emoji ? `${types[commit.type].emoji} ` : ''}${types[commit.type].title}`;
commit.groupType = `${types[commit.type].emoji ? `${types[commit.type].emoji} ` : ''}${types[commit.type].title}`;
} else {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const transform = require('./lib/commit-transform');
* @type {Promise<Object>} preset with `parserOpts` and `writerOpts`.
*/
module.exports = conventionalChangelogAngular.then(preset =>
merge(preset, {writerOpts: {transform, commitGroupsSort}})
merge(preset, {writerOpts: {transform, commitGroupsSort, groupBy: 'groupType'}})
);
44 changes: 20 additions & 24 deletions test/commit-groups-compare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,34 @@ 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'},
{title: '📘 Documentation', commits: [{type: 'docs'}]},
{title: '🐞 Bug Fixes', commits: [{type: 'fix'}]},
{title: 'Features', commits: [{type: 'feat'}]},
];
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'}}},
feat: {title: 'Features'},
fix: {title: 'Bug Fixes'},
docs: {title: 'Documentation'},
},
});

t.deepEqual(commitGroups.sort(compare), [
{title: 'Features'},
{title: 'Bug Fixes'},
{title: 'Documentation'},
{title: 'Initial'},
{title: 'Metadata'},
{title: 'Features', commits: [{type: 'feat'}]},
{title: '🐞 Bug Fixes', commits: [{type: 'fix'}]},
{title: '📘 Documentation', commits: [{type: 'docs'}]},
]);
});

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'},
{title: 'b-Test', commits: [{type: 'btest'}]},
{title: 'z-Test', commits: [{type: 'ztest'}]},
{title: 'Bug Fixes', commits: [{type: 'fix'}]},
{title: 'z-Test', commits: [{type: 'ztest'}]},
{title: 'a-Test', commits: [{type: 'atest'}]},
{title: 'Features', commits: [{type: 'feat'}]},
];
const compare = commitGroupsCompare({
typesOrder: ['feat', 'fix'],
Expand All @@ -48,11 +44,11 @@ test('Return alphabeticaly ordered commit groups not in "typesOrder" at the end
});

t.deepEqual(commitGroups.sort(compare), [
{title: 'Features'},
{title: 'Bug Fixes'},
{title: 'a-Test'},
{title: 'b-Test'},
{title: 'z-Test'},
{title: 'z-Test'},
{title: 'Features', commits: [{type: 'feat'}]},
{title: 'Bug Fixes', commits: [{type: 'fix'}]},
{title: 'a-Test', commits: [{type: 'atest'}]},
{title: 'b-Test', commits: [{type: 'btest'}]},
{title: 'z-Test', commits: [{type: 'ztest'}]},
{title: 'z-Test', commits: [{type: 'ztest'}]},
]);
});
28 changes: 19 additions & 9 deletions test/commit-transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ const COMMIT_HASH_LENGTH = 7;
test('Return transformed commit if type has "changelog" "true"', t => {
const commit = transform({type: 'feat', hash: '1234567890'}, {feat: {title: 'Feature title', changelog: true}});

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
});

test('Return transformed commit and truncate hash', t => {
const commit = transform({type: 'feat', hash: '1234567890'}, {feat: {title: 'Feature title', changelog: true}});

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
t.is(commit.hash.length, COMMIT_HASH_LENGTH);
t.true('1234567890'.startsWith(commit.hash));
});
Expand All @@ -35,7 +37,8 @@ test('Return transformed commit if it has a breaking change', t => {
{feat: {title: 'Feature title', changelog: false}}
);

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
});

test('Set notes title to "Breaking changes" if commit has a breaking change', t => {
Expand All @@ -44,20 +47,23 @@ test('Set notes title to "Breaking changes" if commit has a breaking change', t
{feat: {title: 'Feature title', changelog: false}}
);

t.is(commit.type, 'feat');
t.is(commit.notes[0].title, 'Breaking changes');
});

test('Return transformed commit and preserve "scope"', t => {
const commit = transform({type: 'feat', scope: 'scope1'}, {feat: {title: 'Feature title', changelog: true}});

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
t.is(commit.scope, 'scope1');
});

test('Return transformed commit and remove "scope" if "*"', t => {
const commit = transform({type: 'feat', scope: '*'}, {feat: {title: 'Feature title', changelog: true}});

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
t.falsy(commit.scope);
});

Expand All @@ -68,7 +74,8 @@ test('Transform reference links in subject', t => {
{host: 'https://github.com', owner: 'github_user', repository: 'repo_name'}
);

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
t.is(
commit.subject,
'Subject, closes [#123](https://github.com/github_user/repo_name/issues/123), fix [#456](https://github.com/github_user/repo_name/issues/456)'
Expand All @@ -82,7 +89,8 @@ test('Transform reference link in subject (with repoUrl)', t => {
{repoUrl: 'https://github.com/github_user/repo_name'}
);

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
t.is(
commit.subject,
'Subject, closes [#123](https://github.com/github_user/repo_name/issues/123), fix [#456](https://github.com/github_user/repo_name/issues/456)'
Expand All @@ -96,7 +104,8 @@ test('Remove reference if already present in subject', t => {
{repoUrl: 'https://github.com/github_user/repo_name'}
);

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
t.is(commit.references.length, 1);
t.deepEqual(commit.references[0], {issue: '456'});
});
Expand All @@ -108,6 +117,7 @@ test('Transform mention link in subject', t => {
{host: 'https://github.com'}
);

t.is(commit.type, 'Feature title');
t.is(commit.type, 'feat');
t.is(commit.groupType, 'Feature title');
t.is(commit.subject, 'Subject, [@username](https://github.com/username) [@username2](https://github.com/username2)');
});
14 changes: 9 additions & 5 deletions test/preset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,19 @@ test.serial('Create mention link', async t => {

test.serial('Print commit group in order', async t => {
const log = await changelog(
['docs(scope1): Some doc update', 'fix(scope1): First fix', 'feat(scope2): Second feature'],
['docs(scope1): Some doc update', 'fix(scope1): First fix', 'feat(scope2): Second feature', 'chore: some chore'],
{
types: {
feat: {title: 'Feature title', changelog: true},
fix: {title: 'Fix title', changelog: true},
docs: {title: 'Documentation title', changelog: true},
chore: {title: 'Chores', changelog: true, emoji: '♻️'},
fix: {title: 'Fix title', changelog: true, emoji: '🐛'},
docs: {title: 'Documentation title', changelog: true, emoji: '📚'},
feat: {title: 'Feature title', changelog: true, emoji: '✨'},
},
}
);

t.regex(log, /[\S\s]*### Feature title[\S\s]*### Fix title[\S\s]*### Documentation title/);
t.regex(
log,
/[\S\s]*### ✨ Feature title[\S\s]*### 🐛 Fix title[\S\s]*### 📚 Documentation title[\S\s]*### ♻️ Chores/
);
});

0 comments on commit 2a07254

Please sign in to comment.