From 5101e9085c3c2694104ab67b3285cdb741f2552f Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 29 Feb 2016 22:08:23 +0100 Subject: [PATCH] Add tests and the possibility to specify template --- README.md | 9 ++-- cli.js | 7 ++-- lib/addContributor.js | 7 ++-- lib/addContributor.test.js | 85 ++++++++++++++++++++++++++++++++++++++ package.json | 9 +++- 5 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 lib/addContributor.test.js diff --git a/README.md b/README.md index 862d87e..284ab06 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ 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)): +- `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: ๐Ÿ”ง @@ -53,8 +53,9 @@ or creating a `all-contributors` updating the `package.json` file: ``` 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' -- `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. -- `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). diff --git a/cli.js b/cli.js index a1c4d4e..60f63aa 100755 --- a/cli.js +++ b/cli.js @@ -13,7 +13,8 @@ var addContributor = require('./lib/addContributor'); var defaultRCFile = '.all-contributorsrc'; var argv = require('yargs') - .usage('Usage: $0 ') + .command('add', 'add a new contributor') + .usage('Usage: $0 add ') .demand(2) .default('config', defaultRCFile) .default('file', 'README.md') @@ -32,8 +33,8 @@ var argv = require('yargs') .argv; argv.emoji = assign({}, defaultEmojis, argv.emoji); -argv.username = argv._[0]; -argv.contributions = argv._[1].split(','); +argv.username = argv._[1]; +argv.contributions = argv._[2].split(','); argv.file = path.join(__dirname, argv.file); getUserInfo(argv.username, function(error, user) { diff --git a/lib/addContributor.js b/lib/addContributor.js index dae48eb..f8259c5 100644 --- a/lib/addContributor.js +++ b/lib/addContributor.js @@ -11,11 +11,10 @@ function listAllContributors(start, lines) { return lines.slice(start, start + i); } -var contributionTemplate = template( +var defaultTemplate = '[![<%= 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 %>)' -); + ' | [<%= contributions %>](https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= user.login %>)'; function contributorEntry(options, user) { var contributions = options.contributions @@ -24,6 +23,8 @@ function contributorEntry(options, user) { }) .join(''); + var contributionTemplate = template(options.template ||ย defaultTemplate); + return contributionTemplate({ user: user, contributions: contributions, diff --git a/lib/addContributor.test.js b/lib/addContributor.test.js new file mode 100644 index 0000000..290e2c0 --- /dev/null +++ b/lib/addContributor.test.js @@ -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)
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)
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)
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)
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)
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)
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); +}); diff --git a/package.json b/package.json index 9e926e3..c94e792 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,10 @@ "version": "1.0.0", "description": "Tool to easily add recognition for new contributors", "bin": "cli.js", - "scripts": {}, + "scripts": { + "test": "ava lib/**/*.test.js", + "test:w": "npm test -- --watch" + }, "repository": { "type": "git", "url": "git+https://github.com/jfmengels/all-contributors-cli.git" @@ -26,12 +29,14 @@ "yargs": "^4.2.0" }, "all-contributors": { - "file": "./fixture/some.md", "projectOwner": "jfmengels", "projectName": "all-contributors-cli", "imageSize": 100, "emoji": { "cheerful": ":smiley:" } + }, + "devDependencies": { + "ava": "^0.12.0" } }