mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 13:36:29 +00:00
Find and replace badges, drop badge comment tag.
This commit is contained in:
parent
6c90a4567f
commit
6ab2b5d61a
5 changed files with 40 additions and 21 deletions
|
@ -1,6 +1,6 @@
|
|||
# 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.
|
||||
|
||||
|
@ -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
|
||||
```
|
||||
|
|
|
@ -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 = '<!-- ALL-CONTRIBUTORS-' + tag + ':';
|
||||
var tagToLookFor = '<!-- ALL-CONTRIBUTORS-LIST:';
|
||||
var closingTag = '-->';
|
||||
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);
|
||||
};
|
||||
|
|
|
@ -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',
|
||||
'<!-- ALL-CONTRIBUTORS-BADGE:START --><!-- ALL-CONTRIBUTORS-BADGE:END -->',
|
||||
'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-BADGE:START -->[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)<!-- ALL-CONTRIBUTORS-BADGE:END -->',
|
||||
'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);
|
||||
});
|
||||
|
|
|
@ -11,7 +11,7 @@ test('should insert badge under title', t => {
|
|||
].join('\n');
|
||||
const expected = [
|
||||
'# 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',
|
||||
'',
|
||||
|
@ -27,7 +27,7 @@ test('should add badge if content is empty', t => {
|
|||
const content = '';
|
||||
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');
|
||||
|
||||
const result = addBadge(content);
|
||||
|
|
|
@ -3,12 +3,11 @@
|
|||
var _ = require('lodash/fp');
|
||||
var injectContentBetween = require('../markdown').injectContentBetween;
|
||||
|
||||
function newContent(type) {
|
||||
return '<!-- ALL-CONTRIBUTORS-' + type + ':START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-' + type + ':END -->';
|
||||
}
|
||||
var badgeContent = '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors)';
|
||||
var listContent = '<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-LIST:END -->';
|
||||
|
||||
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 = {
|
||||
|
|
Loading…
Reference in a new issue