commit 2a0607ee2a82d32e8a21c2b60526b0a24035bacf Author: Jeroen Engels Date: Mon Feb 29 01:48:55 2016 +0100 Initial commit: working markdown edit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..dd9dbfa --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# all-contributors-cli + +This is a tool to help automate adding contributor acknowledgements according to the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. + +## Usage + +TODO + +## Configuration + +TODO diff --git a/cli.js b/cli.js new file mode 100644 index 0000000..5263cda --- /dev/null +++ b/cli.js @@ -0,0 +1,81 @@ +var options = { + contributingFile: './fixture/some.md', + projectOwner: 'jfmengels', + projectName: 'all-contributors-cli', + imageSize: 130 +}; + +var fs = require('fs'); +var path = require('path'); +var request = require('request'); +var findIndex = require('lodash.findindex'); + +function getUserInfo(username, cb) { + request.get({ + url: 'https://api.github.com/users/' + username, + headers: { + 'User-Agent': 'request' + } + }, function(error, res) { + if (error) { + return cb(error); + } + return cb(null, JSON.parse(res.body)); + }); +} + +function readMarkdown(filePath, cb) { + fs.readFile(path.join(__dirname, filePath), 'utf8', cb); +} + +function writeMarkdown(filePath, content, cb) { + fs.writeFile(path.join(__dirname, filePath), content, cb); +} + +function listAllContributors(start, lines) { + var i = 0; + while (start + i < lines.length && lines[start + i].indexOf('[![') === 0) { + i++; + } + return lines.slice(start, start + i); +} + +function contributorEntry(options, user) { + return '[![' + user.name + '](' + user.avatar_url + '&s=' + options.imageSize + ')' + + '
' + user.name + '](' + user.html_url + ')' + + ' | [📖](https://github.com/' + options.projectOwner + '/' + options.projectName + '/commits?author=' + user.login + ')'; +} + +function addContributor(contributor, fileContent) { + var lines = fileContent.split('\n'); + var contributorListStart = findIndex(lines, function(line) { + return line.indexOf(':---: | :---:') !== -1; + }); + var contributors = [].concat( + listAllContributors(contributorListStart + 1, lines), + contributor + ); + + return [].concat( + lines.slice(0, contributorListStart + 1), + contributors, + lines.slice(contributorListStart + contributors.length) + ).join('\n') +} + +getUserInfo('jfmengels', function(error, user) { + if (error) { + return console.error(error); + } + readMarkdown(options.contributingFile, function(error, fileContent) { + if (error) { + return console.error(error); + } + var newFileContent = addContributor(contributorEntry(options, user), fileContent); + writeMarkdown(options.contributingFile, newFileContent, function(error, fileContent) { + if (error) { + return console.error(error); + } + }); + }); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..0d0f082 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "all-contributors-cli", + "version": "1.0.0", + "description": "Tool to easily add recognition for new contributors", + "bin": "cli.js", + "scripts": {}, + "repository": { + "type": "git", + "url": "git+https://github.com/jfmengels/all-contributors-cli.git" + }, + "keywords": [ + "all-contributors", + "contributors" + ], + "author": "Jeroen Engels ", + "license": "MIT", + "bugs": { + "url": "https://github.com/jfmengels/all-contributors-cli/issues" + }, + "homepage": "https://github.com/jfmengels/all-contributors-cli#readme", + "dependencies": { + "lodash.findindex": "^4.2.0", + "request": "^2.69.0" + } +}