diff --git a/README.md b/README.md index bfa7fb4..554eb94 100644 --- a/README.md +++ b/README.md @@ -5,35 +5,49 @@ This is a tool to help automate adding contributor acknowledgements according to ## Usage ``` +# Add new contributor , who a contribution of type all-contributors ``` Where: - `username` is the user's GitHub username -- `contribution` is a ';'-separated list of ways to contribute, from the following list ([see the specs](https://github.com/kentcdodds/all-contributors#emoji-key)): -- code: 💻 -- plugin: 🔌 -- tool: 🔧 -- doc: 📖 -- question: ❓ -- test: ⚠️ -- bug: 🐛 -- example: 💡 -- blog: 📝 -- tutorial: ✅ -- video: 📹 -- talk: 📢 -- design: 🎨 +- `contribution` is a ','-separated list of ways to contribute, from the following list ([see the specs](https://github.com/kentcdodds/all-contributors#emoji-key)): + - code: 💻 + - plugin: 🔌 + - tool: 🔧 + - doc: 📖 + - question: ❓ + - test: ⚠️ + - bug: 🐛 + - example: 💡 + - blog: 📝 + - tutorial: ✅ + - video: 📹 + - talk: 📢 + - design: 🎨 + - review: 👀 ## Configuration -You can configure the project by creating a `.all-contributors` JSON file. +You can configure the project by creating a `.all-contributorsrc` JSON file. ```json { "file": "README.md", "owner": "jfmengels", "emoji": { - "thoughtleadering": "some emoji" + "cheerful": ":smiley:" + } +} +``` +or creating a `all-contributors` updating the `package.json` file: + +```json +{ + "name": "all-contributors-cli", + "...": "...", + "all-contributors": { + "file": "README.md", + "owner": "jfmengels" } } ``` diff --git a/cli.js b/cli.js old mode 100644 new mode 100755 index 5263cda..a1c4d4e --- a/cli.js +++ b/cli.js @@ -1,78 +1,51 @@ -var options = { - contributingFile: './fixture/some.md', - projectOwner: 'jfmengels', - projectName: 'all-contributors-cli', - imageSize: 130 -}; +#!/usr/bin/env node +'use strict'; var fs = require('fs'); var path = require('path'); -var request = require('request'); -var findIndex = require('lodash.findindex'); +var assign = require('lodash.assign'); -function getUserInfo(username, cb) { - request.get({ - url: 'https://api.github.com/users/' + username, - headers: { - 'User-Agent': 'request' +var markdown = require('./lib/markdown'); +var getUserInfo = require('./lib/github'); +var defaultEmojis = require('./lib/emoji'); +var addContributor = require('./lib/addContributor'); + +var defaultRCFile = '.all-contributorsrc'; + +var argv = require('yargs') + .usage('Usage: $0 ') + .demand(2) + .default('config', defaultRCFile) + .default('file', 'README.md') + .config('config', function(configPath) { + try { + return JSON.parse(fs.readFileSync(configPath, 'utf-8')); + } catch (error) { + if (configPath !== path.join(__dirname, defaultRCFile)) { + console.error(error.message); + process.exit(1); + } } - }, function(error, res) { - if (error) { - return cb(error); - } - return cb(null, JSON.parse(res.body)); - }); -} + }) + .default('emoji', {}) + .pkgConf('all-contributors') + .argv; -function readMarkdown(filePath, cb) { - fs.readFile(path.join(__dirname, filePath), 'utf8', cb); -} +argv.emoji = assign({}, defaultEmojis, argv.emoji); +argv.username = argv._[0]; +argv.contributions = argv._[1].split(','); +argv.file = path.join(__dirname, argv.file); -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) { +getUserInfo(argv.username, function(error, user) { if (error) { return console.error(error); } - readMarkdown(options.contributingFile, function(error, fileContent) { + markdown.read(argv.file, function(error, fileContent) { if (error) { return console.error(error); } - var newFileContent = addContributor(contributorEntry(options, user), fileContent); - writeMarkdown(options.contributingFile, newFileContent, function(error, fileContent) { + var newFileContent = addContributor(argv, user, fileContent); + markdown.write(argv.file, newFileContent, function(error, fileContent) { if (error) { return console.error(error); } diff --git a/lib/addContributor.js b/lib/addContributor.js new file mode 100644 index 0000000..dae48eb --- /dev/null +++ b/lib/addContributor.js @@ -0,0 +1,51 @@ +'use strict'; + +var template = require('lodash.template'); +var findIndex = require('lodash.findindex'); + +function listAllContributors(start, lines) { + var i = 0; + while (start + i < lines.length && lines[start + i].indexOf('[![') === 0) { + i++; + } + return lines.slice(start, start + i); +} + +var contributionTemplate = template( + '[![<%= user.name %>](<%= user.avatar_url %>&s=<%= options.imageSize %>)' + + '
<%= user.name %>](<%= user.html_url %>)' + + ' | [<%= contributions %>](https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= user.login %>)' +); + +function contributorEntry(options, user) { + var contributions = options.contributions + .map(function(contribution) { + return options.emoji[contribution]; + }) + .join(''); + + return contributionTemplate({ + user: user, + contributions: contributions, + options: options + }); +} + +module.exports = function addContributor(options, user, fileContent) { + var contributor = contributorEntry(options, user); + + 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') +} diff --git a/lib/emoji.js b/lib/emoji.js new file mode 100644 index 0000000..3fd96d2 --- /dev/null +++ b/lib/emoji.js @@ -0,0 +1,18 @@ +'use strict'; + +module.exports = { + code: '💻', + plugin: '🔌', + tool: '🔧', + doc: '📖', + question: '❓', + test: '⚠️', + bug: '🐛', + example: '💡', + blog: '📝', + tutorial: '✅', + video: '📹', + talk: '📢', + design: '🎨', + review: '👀' +}; diff --git a/lib/github.js b/lib/github.js new file mode 100644 index 0000000..5396495 --- /dev/null +++ b/lib/github.js @@ -0,0 +1,17 @@ +'use strict'; + +var request = require('request'); + +module.exports = 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)); + }); +} diff --git a/lib/markdown.js b/lib/markdown.js new file mode 100644 index 0000000..053799f --- /dev/null +++ b/lib/markdown.js @@ -0,0 +1,16 @@ +'use strict'; + +var fs = require('fs'); + +function read(filePath, cb) { + fs.readFile(filePath, 'utf8', cb); +} + +function write(filePath, content, cb) { + fs.writeFile(filePath, content, cb); +} + +module.exports = { + read: read, + write: write +} diff --git a/package.json b/package.json index 0d0f082..9e926e3 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,19 @@ }, "homepage": "https://github.com/jfmengels/all-contributors-cli#readme", "dependencies": { + "lodash.assign": "^4.0.4", "lodash.findindex": "^4.2.0", - "request": "^2.69.0" + "lodash.template": "^4.2.1", + "request": "^2.69.0", + "yargs": "^4.2.0" + }, + "all-contributors": { + "file": "./fixture/some.md", + "projectOwner": "jfmengels", + "projectName": "all-contributors-cli", + "imageSize": 100, + "emoji": { + "cheerful": ":smiley:" + } } }