Add tests and the possibility to specify template

This commit is contained in:
Jeroen Engels 2016-02-29 22:08:23 +01:00
parent d44ac1a21f
commit 5101e9085c
5 changed files with 105 additions and 12 deletions

View file

@ -10,7 +10,7 @@ all-contributors <username> <contribution>
``` ```
Where: Where:
- `username` is the user's GitHub username - `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)): - `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: 💻 - code: 💻
- plugin: 🔌 - plugin: 🔌
- tool: 🔧 - tool: 🔧
@ -53,8 +53,9 @@ or creating a `all-contributors` updating the `package.json` file:
``` ```
These are the keys you can specify: These are the keys you can specify:
- `emoji`: Specify custom emoji, can override the documented emojis. It doesn't really have to be emojis really.
- `file`: File to write the list of contributors in. Default: 'README.md' - `file`: File to write the list of contributors in. Default: 'README.md'
- `owner`: Name of the user the project is hosted by. Example: `jfmengels/all-contributor-cli` --> `jfmengels`. By default will be parsed from the repo's homepage in `package.json` (TODO).
- `project`: Name of the project. Default: Name of the project written in the `package.json` file (TODO).
- `imageSize`: Size (in px) of the user's avatar. Default: 100. - `imageSize`: Size (in px) of the user's avatar. Default: 100.
- `emoji`: Specify custom emoji, can override the documented emojis. - `owner`: Name of the user the project is hosted by. Example: `jfmengels/all-contributor-cli` --> `jfmengels`. By default will be parsed from the repo's homepage in `package.json` (TODO).
- `project`: Name of the project. Example: `jfmengels/all-contributor-cli` --> `all-contributor-cli`. Default: Name of the project written in the `package.json` file (TODO).
- `template`: Define your own contributor template. Please read the code to see what you can define (**warning**: not sure it will work well after several tries).

7
cli.js
View file

@ -13,7 +13,8 @@ var addContributor = require('./lib/addContributor');
var defaultRCFile = '.all-contributorsrc'; var defaultRCFile = '.all-contributorsrc';
var argv = require('yargs') var argv = require('yargs')
.usage('Usage: $0 <username> <contribution>') .command('add', 'add a new contributor')
.usage('Usage: $0 add <username> <contribution>')
.demand(2) .demand(2)
.default('config', defaultRCFile) .default('config', defaultRCFile)
.default('file', 'README.md') .default('file', 'README.md')
@ -32,8 +33,8 @@ var argv = require('yargs')
.argv; .argv;
argv.emoji = assign({}, defaultEmojis, argv.emoji); argv.emoji = assign({}, defaultEmojis, argv.emoji);
argv.username = argv._[0]; argv.username = argv._[1];
argv.contributions = argv._[1].split(','); argv.contributions = argv._[2].split(',');
argv.file = path.join(__dirname, argv.file); argv.file = path.join(__dirname, argv.file);
getUserInfo(argv.username, function(error, user) { getUserInfo(argv.username, function(error, user) {

View file

@ -11,11 +11,10 @@ function listAllContributors(start, lines) {
return lines.slice(start, start + i); return lines.slice(start, start + i);
} }
var contributionTemplate = template( var defaultTemplate =
'[![<%= user.name %>](<%= user.avatar_url %>&s=<%= options.imageSize %>)' + '[![<%= user.name %>](<%= user.avatar_url %>&s=<%= options.imageSize %>)' +
'<br /><%= user.name %>](<%= user.html_url %>)' + '<br /><%= user.name %>](<%= user.html_url %>)' +
' | [<%= contributions %>](https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= user.login %>)' ' | [<%= contributions %>](https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= user.login %>)';
);
function contributorEntry(options, user) { function contributorEntry(options, user) {
var contributions = options.contributions var contributions = options.contributions
@ -24,6 +23,8 @@ function contributorEntry(options, user) {
}) })
.join(''); .join('');
var contributionTemplate = template(options.template || defaultTemplate);
return contributionTemplate({ return contributionTemplate({
user: user, user: user,
contributions: contributions, contributions: contributions,

View file

@ -0,0 +1,85 @@
import test from 'ava';
import addContributor from './addContributor';
function getUserLine(content, {login}) {
return content
.split('\n')
.filter(line => line.indexOf(login) !== -1)
[0];
}
function fixtures() {
const options = {
projectOwner: 'kentcdodds',
projectName: 'all-contributors',
imageSize: 100,
contributions: ['doc'],
emoji: {
code: ':code:',
doc: ':doc:',
test: ':test:'
}
};
const user = {
login: 'jfmengels',
name: 'Jeroen Engels',
html_url: 'https://github.com/jfmengels',
avatar_url: 'https://avatars.githubusercontent.com/u/3869412?v=3'
};
const content = `
# project
Description
## Contributors
These people contributed to the project:
:---: | :---:
[![Kent C. Dodds](https://avatars1.githubusercontent.com/u/1500684?s=130)<br />Kent C. Dodds](http://kentcdodds.com) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds)
[![Divjot Singh](https://avatars1.githubusercontent.com/u/6177621?s=130)<br />Divjot Singh](http://bogas04.github.io) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=bogas04)
Thanks a lot guys!
`;
return {options, user, content};
}
test('should add a new contributor to the end of the list', t => {
const {options, user, content} = fixtures();
const expected = `
# project
Description
## Contributors
These people contributed to the project:
:---: | :---:
[![Kent C. Dodds](https://avatars1.githubusercontent.com/u/1500684?s=130)<br />Kent C. Dodds](http://kentcdodds.com) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds)
[![Divjot Singh](https://avatars1.githubusercontent.com/u/6177621?s=130)<br />Divjot Singh](http://bogas04.github.io) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=bogas04)
[![Jeroen Engels](https://avatars.githubusercontent.com/u/3869412?v=3&s=100)<br />Jeroen Engels](https://github.com/jfmengels) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=jfmengels)
Thanks a lot guys!
`;
t.is(addContributor(options, user, content), expected);
});
test('should be able to inject several contributions', t => {
const {options, user, content} = fixtures();
options.contributions = ['doc', 'code'];
const expected = '[![Jeroen Engels](https://avatars.githubusercontent.com/u/3869412?v=3&s=100)<br />Jeroen Engels](https://github.com/jfmengels) | [:doc::code:](https://github.com/kentcdodds/all-contributors/commits?author=jfmengels)';
const result = addContributor(options, user, content);
t.is(getUserLine(result, user), expected);
});
test('should be able to specify a new template in options', t => {
const {options, user, content} = fixtures();
options.template = '<%= user.login %> made awesome contributions!';
const expected = 'jfmengels made awesome contributions!';
const result = addContributor(options, user, content);
t.is(getUserLine(result, user), expected);
});

View file

@ -3,7 +3,10 @@
"version": "1.0.0", "version": "1.0.0",
"description": "Tool to easily add recognition for new contributors", "description": "Tool to easily add recognition for new contributors",
"bin": "cli.js", "bin": "cli.js",
"scripts": {}, "scripts": {
"test": "ava lib/**/*.test.js",
"test:w": "npm test -- --watch"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/jfmengels/all-contributors-cli.git" "url": "git+https://github.com/jfmengels/all-contributors-cli.git"
@ -26,12 +29,14 @@
"yargs": "^4.2.0" "yargs": "^4.2.0"
}, },
"all-contributors": { "all-contributors": {
"file": "./fixture/some.md",
"projectOwner": "jfmengels", "projectOwner": "jfmengels",
"projectName": "all-contributors-cli", "projectName": "all-contributors-cli",
"imageSize": 100, "imageSize": 100,
"emoji": { "emoji": {
"cheerful": ":smiley:" "cheerful": ":smiley:"
} }
},
"devDependencies": {
"ava": "^0.12.0"
} }
} }