diff --git a/cli.js b/cli.js index 2ce2ff0..e02cdb2 100755 --- a/cli.js +++ b/cli.js @@ -55,6 +55,19 @@ function startGeneration(argv, cb) { }); } +function addContribution(argv, cb) { + var username = argv._[1]; + var contributions = argv._[2]; + // Add or update contributor in the config file + updateContributors(argv, username, contributions, function (error, contributors) { + if (error) { + return onError(error); + } + argv.contributors = contributors; + startGeneration(argv, cb); + }); +} + function onError(error) { if (error) { return console.error(error); @@ -68,14 +81,5 @@ if (command === 'init') { } else if (command === 'generate') { startGeneration(argv, onError); } else if (command === 'add') { - var username = argv._[1]; - var contributions = argv._[2]; - // Add or update contributor in the config file - updateContributors(argv, username, contributions, function (error, contributors) { - if (error) { - return onError(error); - } - argv.contributors = contributors; - startGeneration(argv, onError); - }); + addContribution(argv, onError); } diff --git a/lib/contributors/add.js b/lib/contributors/add.js index a5ddc53..cda2c05 100644 --- a/lib/contributors/add.js +++ b/lib/contributors/add.js @@ -6,8 +6,7 @@ function uniqueTypes(contribution) { return contribution.type || contribution; } -function formatContributions(options, existing, newTypes) { - var types = newTypes.split(','); +function formatContributions(options, existing, types) { if (options.url) { return (existing || []).concat(types.map(function (type) { return {type: type, url: options.url}; diff --git a/lib/contributors/add.test.js b/lib/contributors/add.test.js index d44d05d..fc75ce2 100644 --- a/lib/contributors/add.test.js +++ b/lib/contributors/add.test.js @@ -51,7 +51,7 @@ test.cb('should callback with error if infoFetcher fails', t => { test.cb('should add new contributor at the end of the list of contributors', t => { const {options} = fixtures(); const username = 'login3'; - const contributions = 'doc'; + const contributions = ['doc']; return addContributor(options, username, contributions, mockInfoFetcher, function (error, contributors) { t.notOk(error); @@ -72,7 +72,7 @@ test.cb('should add new contributor at the end of the list of contributors', t = test.cb('should add new contributor at the end of the list of contributors with a url link', t => { const {options} = fixtures(); const username = 'login3'; - const contributions = 'doc'; + const contributions = ['doc']; options.url = 'www.foo.bar'; return addContributor(options, username, contributions, mockInfoFetcher, function (error, contributors) { @@ -94,7 +94,7 @@ test.cb('should add new contributor at the end of the list of contributors with test.cb(`should not update an existing contributor's contributions where nothing has changed`, t => { const {options} = fixtures(); const username = 'login2'; - const contributions = 'blog,code'; + const contributions = ['blog', 'code']; return addContributor(options, username, contributions, mockInfoFetcher, function (error, contributors) { t.notOk(error); @@ -106,8 +106,7 @@ test.cb(`should not update an existing contributor's contributions where nothing test.cb(`should update an existing contributor's contributions if a new type is added`, t => { const {options} = fixtures(); const username = 'login1'; - const contributions = 'bug'; - + const contributions = ['bug']; return addContributor(options, username, contributions, mockInfoFetcher, function (error, contributors) { t.notOk(error); t.is(contributors.length, 2); @@ -128,7 +127,7 @@ test.cb(`should update an existing contributor's contributions if a new type is test.cb(`should update an existing contributor's contributions if a new type is added with a link`, t => { const {options} = fixtures(); const username = 'login1'; - const contributions = 'bug'; + const contributions = ['bug']; options.url = 'www.foo.bar'; return addContributor(options, username, contributions, mockInfoFetcher, function (error, contributors) { diff --git a/lib/contributors/index.js b/lib/contributors/index.js index fb3904f..cddf8e1 100644 --- a/lib/contributors/index.js +++ b/lib/contributors/index.js @@ -1,16 +1,19 @@ 'use strict'; +var util = require('../util'); var add = require('./add'); var github = require('./github'); -var configFile = require('../util').configFile; +var prompt = require('./prompt'); module.exports = function addContributor(options, username, contributions, cb) { - add(options, username, contributions, github, function (error, contributors) { - if (error) { - return cb(error); - } - configFile.writeContributors(options.config, contributors, function (error) { - return cb(error, contributors); + prompt(options, username, contributions, function (answers) { + add(options, answers.username, answers.contributions, github, function (error, contributors) { + if (error) { + return cb(error); + } + util.configFile.writeContributors(options.config, contributors, function (error) { + return cb(error, contributors); + }); }); }); }; diff --git a/lib/contributors/prompt.js b/lib/contributors/prompt.js new file mode 100644 index 0000000..93f45e1 --- /dev/null +++ b/lib/contributors/prompt.js @@ -0,0 +1,32 @@ +'use strict'; + +var _ = require('lodash/fp'); +var inquirer = require('inquirer'); +var util = require('../util'); + +function getQuestions(options, username, contributions) { + return [{ + type: 'input', + name: 'username', + message: "What is the contributor's GitHub username?", + when: !username + }, { + type: 'checkbox', + name: 'contributions', + message: "What are the contribution types?", + when: !contributions, + choices: Object.keys(util.contributionTypes(options)) + }]; +} + +module.exports = function prompt(options, username, contributions, cb) { + var defaults = { + username: username, + contributions: contributions && contributions.split(',') + }; + var questions = getQuestions(options, username, contributions); + inquirer.prompt(questions, _.flow( + _.assign(defaults), + cb + )); +}; diff --git a/lib/generate/formatContributionType.js b/lib/generate/formatContributionType.js index 1f3c8a1..26393a0 100644 --- a/lib/generate/formatContributionType.js +++ b/lib/generate/formatContributionType.js @@ -1,43 +1,12 @@ 'use strict'; var _ = require('lodash/fp'); +var util = require('../util'); -var linkToCommits = 'https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= contributor.login %>'; -var linkToIssues = 'https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/issues?q=author%3A<%= contributor.login %>'; var linkTemplate = _.template('[<%= symbol %>](<%= url %>)'); -var defaultTypes = { - blog: {symbol: '📝'}, - bug: { - symbol: '🐛', - link: linkToIssues - }, - code: { - symbol: '💻', - link: linkToCommits - }, - design: {symbol: '🎨'}, - doc: { - symbol: '📖', - link: linkToCommits - }, - example: {symbol: '💡'}, - plugin: {symbol: '🔌'}, - question: {symbol: '❓'}, - review: {symbol: '👀'}, - talk: {symbol: '📢'}, - test: { - symbol: '⚠️', - link: linkToCommits - }, - translation: {symbol: '🌍'}, - tool: {symbol: '🔧'}, - tutorial: {symbol: '✅'}, - video: {symbol: '📹'} -}; - function getType(options, contribution) { - var types = _.assign(defaultTypes, options.types); + var types = util.contributionTypes(options); return types[contribution.type || contribution]; } diff --git a/lib/init/prompt.js b/lib/init/prompt.js index d50cc0b..7ba0ecd 100644 --- a/lib/init/prompt.js +++ b/lib/init/prompt.js @@ -2,7 +2,6 @@ var _ = require('lodash/fp'); var inquirer = require('inquirer'); - var getRepoInfo = require('../util').git; var questions = [{ diff --git a/lib/util/contributionTypes.js b/lib/util/contributionTypes.js new file mode 100644 index 0000000..bddfbb0 --- /dev/null +++ b/lib/util/contributionTypes.js @@ -0,0 +1,40 @@ +'use strict'; + +var _ = require('lodash/fp'); + +var linkToCommits = 'https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= contributor.login %>'; +var linkToIssues = 'https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/issues?q=author%3A<%= contributor.login %>'; + +var defaultTypes = { + blog: {symbol: '📝'}, + bug: { + symbol: '🐛', + link: linkToIssues + }, + code: { + symbol: '💻', + link: linkToCommits + }, + design: {symbol: '🎨'}, + doc: { + symbol: '📖', + link: linkToCommits + }, + example: {symbol: '💡'}, + plugin: {symbol: '🔌'}, + question: {symbol: '❓'}, + review: {symbol: '👀'}, + talk: {symbol: '📢'}, + test: { + symbol: '⚠️', + link: linkToCommits + }, + translation: {symbol: '🌍'}, + tool: {symbol: '🔧'}, + tutorial: {symbol: '✅'}, + video: {symbol: '📹'} +}; + +module.exports = function (options) { + return _.assign(defaultTypes, options.types); +}; diff --git a/lib/util/index.js b/lib/util/index.js index 305dcee..e6cfc6c 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -2,6 +2,7 @@ module.exports = { configFile: require('./configFile'), + contributionTypes: require('./contributionTypes'), git: require('./git'), markdown: require('./markdown') };