From 5662c9389d94d8d269a88ceab048e38d05ad6066 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 21 Mar 2016 23:53:02 +0100 Subject: [PATCH] Add badge/contributors list injection --- lib/generate/index.js | 9 +---- lib/init/addBadge.test.js | 36 +++++++++++++++++ lib/init/addContributorsList.test.js | 60 ++++++++++++++++++++++++++++ lib/init/index.js | 29 +++++++++++++- lib/init/initContent.js | 45 +++++++++++++++++++++ lib/markdown.js | 11 ++++- package.json | 1 + 7 files changed, 180 insertions(+), 11 deletions(-) create mode 100644 lib/init/addBadge.test.js create mode 100644 lib/init/addContributorsList.test.js create mode 100644 lib/init/initContent.js diff --git a/lib/generate/index.js b/lib/generate/index.js index 7cc0cb7..04cca5d 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -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) { diff --git a/lib/init/addBadge.test.js b/lib/init/addBadge.test.js new file mode 100644 index 0000000..228e368 --- /dev/null +++ b/lib/init/addBadge.test.js @@ -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', + '', + '', + '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 = [ + '', + '' + ].join('\n'); + + const result = addBadge(content); + + t.is(result, expected); +}); diff --git a/lib/init/addContributorsList.test.js b/lib/init/addContributorsList.test.js new file mode 100644 index 0000000..1cd3ef7 --- /dev/null +++ b/lib/init/addContributorsList.test.js @@ -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', + '', + '' + ].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', + '', + '' + ].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', + '', + '' + ].join('\n'); + + const result = addContributorsList(content); + + t.is(result, expected); +}); diff --git a/lib/init/index.js b/lib/init/index.js index 3e7bd86..1393eb6 100644 --- a/lib/init/index.js +++ b/lib/init/index.js @@ -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); }); }; diff --git a/lib/init/initContent.js b/lib/init/initContent.js new file mode 100644 index 0000000..94a2885 --- /dev/null +++ b/lib/init/initContent.js @@ -0,0 +1,45 @@ +'use strict'; + +var _ = require('lodash/fp'); +var injectContentBetween = require('../markdown').injectContentBetween; + +function newContent(type) { + return ''; +} + +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) +}; diff --git a/lib/markdown.js b/lib/markdown.js index 2feda2a..35f9c8a 100644 --- a/lib/markdown.js +++ b/lib/markdown.js @@ -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 }; diff --git a/package.json b/package.json index f4df20c..94fe637 100644 --- a/package.json +++ b/package.json @@ -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",