From 2a072545c3584f4bbdfe488345eee8b46058a393 Mon Sep 17 00:00:00 2001 From: Pierre-Denis Vanduynslager Date: Sun, 27 Aug 2017 03:13:18 -0400 Subject: [PATCH] fix(preset): Fix changelog group title order with emoji --- src/lib/commit-groups-compare.js | 16 +++++------ src/lib/commit-transform.js | 2 +- src/preset.js | 2 +- test/commit-groups-compare.test.js | 44 ++++++++++++++---------------- test/commit-transform.test.js | 28 +++++++++++++------ test/preset.test.js | 14 ++++++---- 6 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/lib/commit-groups-compare.js b/src/lib/commit-groups-compare.js index a370f09..4cd51b6 100644 --- a/src/lib/commit-groups-compare.js +++ b/src/lib/commit-groups-compare.js @@ -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. @@ -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; diff --git a/src/lib/commit-transform.js b/src/lib/commit-transform.js index 16bf85d..ec0ce17 100644 --- a/src/lib/commit-transform.js +++ b/src/lib/commit-transform.js @@ -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; } diff --git a/src/preset.js b/src/preset.js index 453f169..bb938a1 100644 --- a/src/preset.js +++ b/src/preset.js @@ -7,5 +7,5 @@ const transform = require('./lib/commit-transform'); * @type {Promise} preset with `parserOpts` and `writerOpts`. */ module.exports = conventionalChangelogAngular.then(preset => - merge(preset, {writerOpts: {transform, commitGroupsSort}}) + merge(preset, {writerOpts: {transform, commitGroupsSort, groupBy: 'groupType'}}) ); diff --git a/test/commit-groups-compare.test.js b/test/commit-groups-compare.test.js index eeefa17..8eb3835 100644 --- a/test/commit-groups-compare.test.js +++ b/test/commit-groups-compare.test.js @@ -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'], @@ -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'}]}, ]); }); diff --git a/test/commit-transform.test.js b/test/commit-transform.test.js index 2425ba6..a419f89 100644 --- a/test/commit-transform.test.js +++ b/test/commit-transform.test.js @@ -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)); }); @@ -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 => { @@ -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); }); @@ -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)' @@ -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)' @@ -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'}); }); @@ -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)'); }); diff --git a/test/preset.test.js b/test/preset.test.js index e4fa2d9..af671f5 100644 --- a/test/preset.test.js +++ b/test/preset.test.js @@ -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/ + ); });