diff --git a/src/cli.js b/src/cli.js index b07cfb7..91212b1 100755 --- a/src/cli.js +++ b/src/cli.js @@ -35,6 +35,12 @@ const yargv = yargs .boolean('commit') .default('files', ['README.md']) .default('contributorsPerLine', 7) + .option('contributorsSortAlphabetically', { + type: 'boolean', + default: false, + description: + 'Sort the list of contributors alphabetically in the generated list', + }) .default('contributors', []) .default('config', defaultRCFile) .config('config', configPath => { diff --git a/src/generate/__tests__/index.js b/src/generate/__tests__/index.js index 5a8e3a1..15e5986 100644 --- a/src/generate/__tests__/index.js +++ b/src/generate/__tests__/index.js @@ -60,6 +60,26 @@ test('split contributors into multiples lines when there are too many', () => { expect(result).toMatchSnapshot() }) +test('sorts the list of contributors if contributorsSortAlphabetically=true', () => { + const {kentcdodds, bogas04} = contributors + const {options, jfmengels, content} = fixtures() + + const resultPreSorted = generate( + options, + [bogas04, jfmengels, kentcdodds], + content, + ) + + options.contributorsSortAlphabetically = true + const resultAutoSorted = generate( + options, + [jfmengels, kentcdodds, bogas04], + content, + ) + + expect(resultPreSorted).toEqual(resultAutoSorted) +}) + test('not inject anything if there is no tags to inject content in', () => { const {kentcdodds} = contributors const {options} = fixtures() diff --git a/src/generate/index.js b/src/generate/index.js index 9112535..c5be533 100644 --- a/src/generate/index.js +++ b/src/generate/index.js @@ -45,6 +45,11 @@ function formatLine(contributors) { function generateContributorsList(options, contributors) { return _.flow( + _.sortBy(function(contributor) { + if (options.contributorsSortAlphabetically) { + return contributor.name + } + }), _.map(function formatEveryContributor(contributor) { return formatContributor(options, contributor) }),