mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 13:36:29 +00:00
Add badge/contributors list injection
This commit is contained in:
parent
a33ef2307f
commit
5662c9389d
7 changed files with 180 additions and 11 deletions
|
@ -3,14 +3,7 @@
|
|||
var _ = require('lodash/fp');
|
||||
var formatBadge = require('./formatBadge');
|
||||
var formatContributor = require('./formatContributor');
|
||||
|
||||
function injectContentBetween(lines, content, startIndex, endIndex) {
|
||||
return [].concat(
|
||||
lines.slice(0, startIndex),
|
||||
content,
|
||||
lines.slice(endIndex)
|
||||
);
|
||||
}
|
||||
var injectContentBetween = require('../markdown').injectContentBetween;
|
||||
|
||||
function injectBetweenTags(tag, newContent) {
|
||||
return function (previousContent) {
|
||||
|
|
36
lib/init/addBadge.test.js
Normal file
36
lib/init/addBadge.test.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
import test from 'ava';
|
||||
import {addBadge} from './initContent';
|
||||
|
||||
test('should insert badge under title', t => {
|
||||
const content = [
|
||||
'# project',
|
||||
'',
|
||||
'Description',
|
||||
'',
|
||||
'Foo bar'
|
||||
].join('\n');
|
||||
const expected = [
|
||||
'# project',
|
||||
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-BADGE:END -->',
|
||||
'',
|
||||
'Description',
|
||||
'',
|
||||
'Foo bar'
|
||||
].join('\n');
|
||||
|
||||
const result = addBadge(content);
|
||||
|
||||
t.is(result, expected);
|
||||
});
|
||||
|
||||
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 -->'
|
||||
].join('\n');
|
||||
|
||||
const result = addBadge(content);
|
||||
|
||||
t.is(result, expected);
|
||||
});
|
60
lib/init/addContributorsList.test.js
Normal file
60
lib/init/addContributorsList.test.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
import test from 'ava';
|
||||
import {addContributorsList} from './initContent';
|
||||
|
||||
test('should insert list under contributors section', t => {
|
||||
const content = [
|
||||
'# project',
|
||||
'',
|
||||
'Description',
|
||||
'',
|
||||
'## Contributors',
|
||||
''
|
||||
].join('\n');
|
||||
const expected = [
|
||||
'# project',
|
||||
'',
|
||||
'Description',
|
||||
'',
|
||||
'## Contributors',
|
||||
'',
|
||||
'<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-LIST:END -->'
|
||||
].join('\n');
|
||||
|
||||
const result = addContributorsList(content);
|
||||
|
||||
t.is(result, expected);
|
||||
});
|
||||
|
||||
test('should create contributors section if it is absent', t => {
|
||||
const content = [
|
||||
'# project',
|
||||
'',
|
||||
'Description'
|
||||
].join('\n');
|
||||
const expected = [
|
||||
'# project',
|
||||
'',
|
||||
'Description',
|
||||
'## Contributors',
|
||||
'',
|
||||
'<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-LIST:END -->'
|
||||
].join('\n');
|
||||
|
||||
const result = addContributorsList(content);
|
||||
|
||||
t.is(result, expected);
|
||||
});
|
||||
|
||||
test('should create contributors section if content is empty', t => {
|
||||
const content = '';
|
||||
const expected = [
|
||||
'',
|
||||
'## Contributors',
|
||||
'',
|
||||
'<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --><!-- ALL-CONTRIBUTORS-LIST:END -->'
|
||||
].join('\n');
|
||||
|
||||
const result = addContributorsList(content);
|
||||
|
||||
t.is(result, expected);
|
||||
});
|
|
@ -1,10 +1,35 @@
|
|||
'use strict';
|
||||
|
||||
var _ = require('lodash/fp');
|
||||
var series = require('async/series');
|
||||
|
||||
var prompt = require('./prompt');
|
||||
var configFile = require('../configFile');
|
||||
var markdown = require('../markdown');
|
||||
var initContent = require('./initContent');
|
||||
|
||||
module.exports = function init(cb) {
|
||||
function injectInFile(file, fn, cb) {
|
||||
markdown.read(file, function (error, content) {
|
||||
if (error) {
|
||||
return cb(error);
|
||||
}
|
||||
markdown.write(file, fn(content), cb);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function init(callback) {
|
||||
prompt(function postPrompt(result) {
|
||||
configFile.writeConfig('.all-contributorsrc', result.config, cb);
|
||||
var tasks = [
|
||||
function writeConfig(cb) {
|
||||
configFile.writeConfig('.all-contributorsrc', result.config, cb);
|
||||
},
|
||||
function addContributorsList(cb) {
|
||||
injectInFile(result.contributorFile, initContent.addContributorsList, cb);
|
||||
},
|
||||
result.badgeFile && function addBadge(cb) {
|
||||
injectInFile(result.badgeFile, initContent.addBadge, cb);
|
||||
}
|
||||
];
|
||||
series(_.compact(tasks), callback);
|
||||
});
|
||||
};
|
||||
|
|
45
lib/init/initContent.js
Normal file
45
lib/init/initContent.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
'use strict';
|
||||
|
||||
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 -->';
|
||||
}
|
||||
|
||||
function addBadge(lines) {
|
||||
return injectContentBetween(lines, newContent('BADGE'), 1, 1);
|
||||
}
|
||||
|
||||
function splitAndRejoin(fn) {
|
||||
return _.flow(
|
||||
_.split('\n'),
|
||||
fn,
|
||||
_.join('\n')
|
||||
);
|
||||
}
|
||||
|
||||
var findContributorsSection = _.findIndex(function isContributorsSection(str) {
|
||||
return str
|
||||
.toLowerCase()
|
||||
.indexOf('# contributors') === 1;
|
||||
});
|
||||
|
||||
function addContributorsList(lines) {
|
||||
var toInject = newContent('LIST');
|
||||
var insertionLine = findContributorsSection(lines);
|
||||
if (insertionLine === -1) {
|
||||
return lines
|
||||
.concat([
|
||||
'## Contributors',
|
||||
'',
|
||||
toInject
|
||||
]);
|
||||
}
|
||||
return injectContentBetween(lines, toInject, insertionLine + 2, insertionLine + 2);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addBadge: splitAndRejoin(addBadge),
|
||||
addContributorsList: splitAndRejoin(addContributorsList)
|
||||
};
|
|
@ -10,7 +10,16 @@ function write(filePath, content, cb) {
|
|||
fs.writeFile(filePath, content, cb);
|
||||
}
|
||||
|
||||
function injectContentBetween(lines, content, startIndex, endIndex) {
|
||||
return [].concat(
|
||||
lines.slice(0, startIndex),
|
||||
content,
|
||||
lines.slice(endIndex)
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
read: read,
|
||||
write: write
|
||||
write: write,
|
||||
injectContentBetween: injectContentBetween
|
||||
};
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
},
|
||||
"homepage": "https://github.com/jfmengels/all-contributors-cli#readme",
|
||||
"dependencies": {
|
||||
"async": "^2.0.0-rc.1",
|
||||
"inquirer": "^0.12.0",
|
||||
"lodash": "^4.6.1",
|
||||
"request": "^2.69.0",
|
||||
|
|
Loading…
Reference in a new issue