Find and replace badges, drop badge comment tag.

This commit is contained in:
Jeroen Engels 2016-03-22 19:53:10 +01:00
parent 6c90a4567f
commit 6ab2b5d61a
5 changed files with 40 additions and 21 deletions

View file

@ -1,6 +1,6 @@
# all-contributors-cli # all-contributors-cli
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors)<!-- ALL-CONTRIBUTORS-BADGE:END --> [![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. 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 ```console
all-contributors init 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 ```console
npm install --save-dev all-contributors-cli npm install --save-dev all-contributors-cli
``` ```

View file

@ -5,9 +5,11 @@ var formatBadge = require('./formatBadge');
var formatContributor = require('./formatContributor'); var formatContributor = require('./formatContributor');
var injectContentBetween = require('../markdown').injectContentBetween; var injectContentBetween = require('../markdown').injectContentBetween;
function injectBetweenTags(tag, newContent) { var badgeRegex = /\[\!\[All Contributors\]\([a-zA-Z0-9\-\.\/_\:\?=]+\)\]\(\#\w+\)/;
function injectListBetweenTags(newContent) {
return function (previousContent) { return function (previousContent) {
var tagToLookFor = '<!-- ALL-CONTRIBUTORS-' + tag + ':'; var tagToLookFor = '<!-- ALL-CONTRIBUTORS-LIST:';
var closingTag = '-->'; var closingTag = '-->';
var startOfOpeningTagIndex = previousContent.indexOf(tagToLookFor + 'START'); var startOfOpeningTagIndex = previousContent.indexOf(tagToLookFor + 'START');
var endOfOpeningTagIndex = previousContent.indexOf(closingTag, startOfOpeningTagIndex); var endOfOpeningTagIndex = previousContent.indexOf(closingTag, startOfOpeningTagIndex);
@ -16,7 +18,7 @@ function injectBetweenTags(tag, newContent) {
return previousContent; return previousContent;
} }
return previousContent.slice(0, endOfOpeningTagIndex + closingTag.length) + return previousContent.slice(0, endOfOpeningTagIndex + closingTag.length) +
newContent + '\n' + newContent + '\n' +
previousContent.slice(startOfClosingTagIndex); previousContent.slice(startOfClosingTagIndex);
}; };
} }
@ -45,11 +47,23 @@ function generateContributorsList(options, contributors) {
)(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) { module.exports = function generate(options, contributors, fileContent) {
var contributorsList = generateContributorsList(options, contributors); var contributorsList = generateContributorsList(options, contributors);
var badge = formatBadge(options, contributors); var badge = formatBadge(options, contributors);
return _.flow( return _.flow(
injectBetweenTags('LIST', '\n' + contributorsList + '\n'), injectListBetweenTags(contributorsList),
injectBetweenTags('BADGE', badge) replaceBadge(badge)
)(fileContent); )(fileContent);
}; };

View file

@ -136,27 +136,34 @@ test('should not inject anything if end tag is malformed', t => {
t.is(result, content); 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 {kentcdodds} = contributors;
const {options} = fixtures(); const {options} = fixtures();
const contributorList = [kentcdodds]; const contributorList = [kentcdodds];
const content = [ const content = [
'# project', '# project',
'', '',
'Badges', 'Badges', [
'<!-- ALL-CONTRIBUTORS-BADGE:START --><!-- ALL-CONTRIBUTORS-BADGE:END -->', '[![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' 'License: MIT'
].join('\n'); ].join('\n');
const expected = [ const expected = [
'# project', '# project',
'', '',
'Badges', 'Badges', [
'<!-- ALL-CONTRIBUTORS-BADGE:START -->[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)<!-- ALL-CONTRIBUTORS-BADGE:END -->', '[![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' 'License: MIT'
].join('\n'); ].join('\n');
const result = generate(options, contributorList, content); const result = generate(options, contributorList, content);
t.is(result, expected); t.is(result, expected);
}); });

View file

@ -11,7 +11,7 @@ test('should insert badge under title', t => {
].join('\n'); ].join('\n');
const expected = [ const expected = [
'# project', '# project',
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-BADGE:END -->', '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)',
'', '',
'Description', 'Description',
'', '',
@ -27,7 +27,7 @@ test('should add badge if content is empty', t => {
const content = ''; const content = '';
const expected = [ const expected = [
'', '',
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-BADGE:END -->' '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)'
].join('\n'); ].join('\n');
const result = addBadge(content); const result = addBadge(content);

View file

@ -3,12 +3,11 @@
var _ = require('lodash/fp'); var _ = require('lodash/fp');
var injectContentBetween = require('../markdown').injectContentBetween; var injectContentBetween = require('../markdown').injectContentBetween;
function newContent(type) { var badgeContent = '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)';
return '<!-- ALL-CONTRIBUTORS-' + type + ':START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-' + type + ':END -->'; var listContent = '<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-LIST:END -->';
}
function addBadge(lines) { function addBadge(lines) {
return injectContentBetween(lines, newContent('BADGE'), 1, 1); return injectContentBetween(lines, badgeContent, 1, 1);
} }
function splitAndRejoin(fn) { function splitAndRejoin(fn) {
@ -26,17 +25,16 @@ var findContributorsSection = _.findIndex(function isContributorsSection(str) {
}); });
function addContributorsList(lines) { function addContributorsList(lines) {
var toInject = newContent('LIST');
var insertionLine = findContributorsSection(lines); var insertionLine = findContributorsSection(lines);
if (insertionLine === -1) { if (insertionLine === -1) {
return lines return lines
.concat([ .concat([
'## Contributors', '## Contributors',
'', '',
toInject listContent
]); ]);
} }
return injectContentBetween(lines, toInject, insertionLine + 2, insertionLine + 2); return injectContentBetween(lines, listContent, insertionLine + 2, insertionLine + 2);
} }
module.exports = { module.exports = {