diff --git a/README.md b/README.md index 6c4de30..96976ab 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # all-contributors-cli -[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors) +[![version](https://img.shields.io/npm/v/all-contributors-cli.svg)](http://npm.im/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. @@ -14,7 +14,7 @@ Then init the project using `init` and answering a few questions: ```console all-contributors init ``` -Once initialized, you don't need to have `all-contributors-cli` instaled globally. You can instead save it as a devDependency of your project and add it to your scripts: +Once initialized, you don't need to have `all-contributors-cli` installed globally. You can instead save it as a devDependency of your project and add it to your `package.json` scripts: ```console npm install --save-dev all-contributors-cli ``` diff --git a/lib/generate/index.js b/lib/generate/index.js index 04cca5d..a224168 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -5,9 +5,11 @@ var formatBadge = require('./formatBadge'); var formatContributor = require('./formatContributor'); var injectContentBetween = require('../markdown').injectContentBetween; -function injectBetweenTags(tag, newContent) { +var badgeRegex = /\[\!\[All Contributors\]\([a-zA-Z0-9\-\.\/_\:\?=]+\)\]\(\#\w+\)/; + +function injectListBetweenTags(newContent) { return function (previousContent) { - var tagToLookFor = ''; var startOfOpeningTagIndex = previousContent.indexOf(tagToLookFor + 'START'); var endOfOpeningTagIndex = previousContent.indexOf(closingTag, startOfOpeningTagIndex); @@ -16,7 +18,7 @@ function injectBetweenTags(tag, newContent) { return previousContent; } return previousContent.slice(0, endOfOpeningTagIndex + closingTag.length) + - newContent + + '\n' + newContent + '\n' + previousContent.slice(startOfClosingTagIndex); }; } @@ -45,11 +47,23 @@ function generateContributorsList(options, contributors) { )(contributors); } +function replaceBadge(newContent) { + return function (previousContent) { + var regexResult = badgeRegex.exec(previousContent); + if (!regexResult) { + return previousContent; + } + return previousContent.slice(0, regexResult.index) + + newContent + + previousContent.slice(regexResult.index + regexResult[0].length); + }; +} + module.exports = function generate(options, contributors, fileContent) { var contributorsList = generateContributorsList(options, contributors); var badge = formatBadge(options, contributors); return _.flow( - injectBetweenTags('LIST', '\n' + contributorsList + '\n'), - injectBetweenTags('BADGE', badge) + injectListBetweenTags(contributorsList), + replaceBadge(badge) )(fileContent); }; diff --git a/lib/generate/index.test.js b/lib/generate/index.test.js index 8c85946..4edf6b0 100644 --- a/lib/generate/index.test.js +++ b/lib/generate/index.test.js @@ -136,27 +136,34 @@ test('should not inject anything if end tag is malformed', t => { t.is(result, content); }); -test('should inject badge if the ALL-CONTRIBUTORS-BADGE tag is present', t => { +test('should replace all-contributors badge if present', t => { const {kentcdodds} = contributors; const {options} = fixtures(); const contributorList = [kentcdodds]; const content = [ '# project', '', - 'Badges', - '', + 'Badges', [ + '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', + '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)', + '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)' + ].join(''), '', 'License: MIT' ].join('\n'); const expected = [ '# project', '', - 'Badges', - '[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)', + 'Badges', [ + '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', + '[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)', + '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)' + ].join(''), '', 'License: MIT' ].join('\n'); const result = generate(options, contributorList, content); + t.is(result, expected); }); diff --git a/lib/init/addBadge.test.js b/lib/init/addBadge.test.js index 228e368..85af951 100644 --- a/lib/init/addBadge.test.js +++ b/lib/init/addBadge.test.js @@ -11,7 +11,7 @@ test('should insert badge under title', t => { ].join('\n'); const expected = [ '# project', - '', + '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)', '', 'Description', '', @@ -27,7 +27,7 @@ test('should add badge if content is empty', t => { const content = ''; const expected = [ '', - '' + '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)' ].join('\n'); const result = addBadge(content); diff --git a/lib/init/initContent.js b/lib/init/initContent.js index 94a2885..bc7cb71 100644 --- a/lib/init/initContent.js +++ b/lib/init/initContent.js @@ -3,12 +3,11 @@ var _ = require('lodash/fp'); var injectContentBetween = require('../markdown').injectContentBetween; -function newContent(type) { - return ''; -} +var badgeContent = '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)'; +var listContent = ''; function addBadge(lines) { - return injectContentBetween(lines, newContent('BADGE'), 1, 1); + return injectContentBetween(lines, badgeContent, 1, 1); } function splitAndRejoin(fn) { @@ -26,17 +25,16 @@ var findContributorsSection = _.findIndex(function isContributorsSection(str) { }); function addContributorsList(lines) { - var toInject = newContent('LIST'); var insertionLine = findContributorsSection(lines); if (insertionLine === -1) { return lines .concat([ '## Contributors', '', - toInject + listContent ]); } - return injectContentBetween(lines, toInject, insertionLine + 2, insertionLine + 2); + return injectContentBetween(lines, listContent, insertionLine + 2, insertionLine + 2); } module.exports = {