From f9bb61d7a26248bbcca11ab12b4d5311840247b0 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Sun, 6 Mar 2016 10:40:19 +0100 Subject: [PATCH] Add badge indicating the number of collaborators (fixes #5) --- README.md | 37 ++++++++++---- cli.js | 1 - lib/generate/formatBadge.js | 11 ++++ lib/generate/formatBadge.test.js | 32 ++++++++++++ lib/generate/index.js | 22 +++++--- lib/generate/index.test.js | 88 +++++++++++++++++++++++++++++--- package.json | 7 +-- 7 files changed, 166 insertions(+), 32 deletions(-) create mode 100644 lib/generate/formatBadge.js create mode 100644 lib/generate/formatBadge.test.js diff --git a/README.md b/README.md index 284039a..b5913b7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # all-contributors-cli + +[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors) + + This is a tool to help automate adding contributor acknowledgements according to the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. ## Installation @@ -11,17 +15,6 @@ npm install all-contributors-cli ## Configuration -### Add contributing section - -If you don't already have a Contributing section in a Markdown file, create one. Then add the following comment tags section to it. Don't worry, they're visible only to those that read the raw file. The tags **must** be at the beginning of the line. - -```md -## Contributing - - - -``` - ### Create a `.all-contributorsrc` file You must create a `.all-contributorsrc` JSON file. The data used to generate the contributors list will be stored in here, and you can configure how you want `all-contributors-cli` to generate the list. @@ -50,6 +43,28 @@ These are the keys you can specify: - `imageSize`: Size (in px) of the user's avatar. Default: 100. - `contributorsPerLine`: Maximum number of columns for the contributors table. Default: 7. - `template`: Define your own template to generate the contributor list. +- `badgeTemplate`: Define your own template to generate the badge. + +### Add contributors section + +If you don't already have a Contributors section in a Markdown file, create one. Then add the comment tags section below to it. Don't worry, they're visible only to those that read the raw file. The tags **must** be at the beginning of the line, and each on their separate line. + +```md +## Contributors + + + +``` + +If you wish to add a badge ( [![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors) ) indicating the number of collaborators, add the following tags (again, at the beginning of the line and each on their separate line): + +```md +some-badge + + + +some-other-badge +``` ## Usage diff --git a/cli.js b/cli.js index 98a1354..49e5388 100755 --- a/cli.js +++ b/cli.js @@ -3,7 +3,6 @@ var fs = require('fs'); var path = require('path'); -var assign = require('lodash.assign'); var generate = require('./lib/generate'); var markdown = require('./lib/markdown'); diff --git a/lib/generate/formatBadge.js b/lib/generate/formatBadge.js new file mode 100644 index 0000000..0a3cd4a --- /dev/null +++ b/lib/generate/formatBadge.js @@ -0,0 +1,11 @@ +'use strict'; + +var _ = require('lodash/fp'); + +var defaultTemplate = '[![All Contributors](https://img.shields.io/badge/all_contributors-<%= contributors.length %>-orange.svg?style=flat-square)](#contributors)'; + +module.exports = function formatBadge(options, contributors) { + return _.template(options.badgeTemplate || defaultTemplate)({ + contributors: contributors + }); +}; diff --git a/lib/generate/formatBadge.test.js b/lib/generate/formatBadge.test.js new file mode 100644 index 0000000..9cf913e --- /dev/null +++ b/lib/generate/formatBadge.test.js @@ -0,0 +1,32 @@ +import test from 'ava'; +import _ from 'lodash/fp'; +import formatBadge from './formatBadge'; + +const fixtures = () => { + const options = { + projectOwner: 'jfmengels', + projectName: 'all-contributors-cli', + imageSize: 100 + }; + return {options}; +} + +test('should return badge with the number of contributors', t => { + const options = {}; + const expected8 = + '[![All Contributors](https://img.shields.io/badge/all_contributors-8-orange.svg?style=flat-square)](#contributors)'; + const expected16 = + '[![All Contributors](https://img.shields.io/badge/all_contributors-16-orange.svg?style=flat-square)](#contributors)'; + + t.is(formatBadge(options, _.times(_.constant({}), 8)), expected8); + t.is(formatBadge(options, _.times(_.constant({}), 16)), expected16); +}); + +test('should be able to specify custom badge template', t => { + const options = { + badgeTemplate: 'We have <%= contributors.length %> contributors' + }; + + t.is(formatBadge(options, _.times(_.constant({}), 8)), 'We have 8 contributors'); + t.is(formatBadge(options, _.times(_.constant({}), 16)), 'We have 16 contributors'); +}); diff --git a/lib/generate/index.js b/lib/generate/index.js index 8f9170a..f5d5426 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -1,18 +1,22 @@ 'use strict'; var _ = require('lodash/fp'); +var formatBadge = require('./formatBadge'); var formatContributor = require('./formatContributor'); -function injectBetweenTags(fileContent, newContent) { - var lines = fileContent.split('\n'); - var openingTagIndex = _.findIndex(_.startsWith('', + '', + '', + 'License: MIT' + ].join('\n'); + + const result = generate(options, contributorList, content); + t.is(result, content); +}); + +test('should not inject anything if end tag is malformed', t => { + const {kentcdodds} = contributors; + const {options} = fixtures(); + const contributorList = [kentcdodds]; + const content = [ + '# project', + '', + 'Description', + '', + '', + '', + 'License: MIT' + ].join('\n'); + + const result = generate(options, contributorList, content); + t.is(result, content); +}); + +test('should inject badge if badge tags are present', t => { + const {kentcdodds} = contributors; + const {options} = fixtures(); + const contributorList = [kentcdodds]; + const content = [ + '# project', + '', + 'Badges', + '', + '', + '', + 'License: MIT' + ].join('\n'); + const expected = [ + '# project', + '', + 'Badges', + '', + '[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)', + '', + '', + 'License: MIT' + ].join('\n'); + + const result = generate(options, contributorList, content); + t.is(result, expected); +}); diff --git a/package.json b/package.json index c9029e6..6726d96 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "all-contributors": "cli.js" }, "scripts": { - "test": "ava lib/**/*.test.js", + "test": "ava 'lib/**/*.test.js'", "test:w": "npm test -- --watch" }, "repository": { @@ -25,11 +25,6 @@ "homepage": "https://github.com/jfmengels/all-contributors-cli#readme", "dependencies": { "lodash": "^4.6.1", - "lodash.assign": "^4.0.4", - "lodash.findindex": "^4.2.0", - "lodash.template": "^4.2.1", - "lodash.uniq": "^4.2.0", - "lodash.values": "^4.1.0", "request": "^2.69.0", "yargs": "^4.2.0" },