From f9cdace3760e7c1f9014d8481e31cc46516f42b7 Mon Sep 17 00:00:00 2001 From: Chris Vickery Date: Mon, 6 Nov 2017 15:39:41 -0800 Subject: [PATCH 1/3] Populate contribution choices with current values --- cli.js | 2 +- lib/contributors/index.js | 4 ++-- lib/contributors/prompt.js | 12 +++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cli.js b/cli.js index 42451c1..7bcf1a0 100755 --- a/cli.js +++ b/cli.js @@ -56,7 +56,7 @@ function addContribution(argv) { var username = argv._[1]; var contributions = argv._[2]; // Add or update contributor in the config file - return updateContributors(argv, username, contributions) + return updateContributors(argv, username, contributions, argv.contributors) .then(data => { argv.contributors = data.contributors; return startGeneration(argv) diff --git a/lib/contributors/index.js b/lib/contributors/index.js index 2cb908d..af93b15 100644 --- a/lib/contributors/index.js +++ b/lib/contributors/index.js @@ -10,8 +10,8 @@ function isNewContributor(contributorList, username) { return !_.find({login: username}, contributorList); } -module.exports = function addContributor(options, username, contributions) { - const answersP = prompt(options, username, contributions); +module.exports = function addContributor(options, username, contributions, allContributions) { + const answersP = prompt(options, username, contributions, allContributions); const contributorsP = answersP .then(answers => add(options, answers.username, answers.contributions, github)); diff --git a/lib/contributors/prompt.js b/lib/contributors/prompt.js index 6da458c..d82c8b5 100644 --- a/lib/contributors/prompt.js +++ b/lib/contributors/prompt.js @@ -18,7 +18,7 @@ var contributionChoices = _.flow( }) ); -function getQuestions(options, username, contributions) { +function getQuestions(options, username, contributions, allContributions) { return [{ type: 'input', name: 'username', @@ -29,17 +29,23 @@ function getQuestions(options, username, contributions) { name: 'contributions', message: 'What are the contribution types?', when: !contributions, + default: function (answers) { + // default values for contributions when updating existing users + return allContributions + .filter((entry) => entry.login.toLowerCase() === answers.username.toLowerCase()) + .reduce((memo, entry) => memo.concat(entry.contributions), []); + }, choices: contributionChoices(options), validate: input => input.length ? true : 'Use space to select at least one contribution type.' }]; } -module.exports = function prompt(options, username, contributions) { +module.exports = function prompt(options, username, contributions, allContributions) { var defaults = { username: username, contributions: contributions && contributions.split(',') }; - var questions = getQuestions(options, username, contributions); + var questions = getQuestions(options, username, contributions, allContributions); return inquirer.prompt(questions) .then(_.assign(defaults)); }; From 48b0ed4b8e1b14cdc462ee81c43b3adb83ea1d13 Mon Sep 17 00:00:00 2001 From: Chris Vickery Date: Tue, 7 Nov 2017 09:06:54 -0800 Subject: [PATCH 2/3] Fix error with add from cli, remove unneeded param --- cli.js | 2 +- lib/contributors/index.js | 4 ++-- lib/contributors/prompt.js | 23 ++++++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cli.js b/cli.js index 7bcf1a0..42451c1 100755 --- a/cli.js +++ b/cli.js @@ -56,7 +56,7 @@ function addContribution(argv) { var username = argv._[1]; var contributions = argv._[2]; // Add or update contributor in the config file - return updateContributors(argv, username, contributions, argv.contributors) + return updateContributors(argv, username, contributions) .then(data => { argv.contributors = data.contributors; return startGeneration(argv) diff --git a/lib/contributors/index.js b/lib/contributors/index.js index af93b15..2cb908d 100644 --- a/lib/contributors/index.js +++ b/lib/contributors/index.js @@ -10,8 +10,8 @@ function isNewContributor(contributorList, username) { return !_.find({login: username}, contributorList); } -module.exports = function addContributor(options, username, contributions, allContributions) { - const answersP = prompt(options, username, contributions, allContributions); +module.exports = function addContributor(options, username, contributions) { + const answersP = prompt(options, username, contributions); const contributorsP = answersP .then(answers => add(options, answers.username, answers.contributions, github)); diff --git a/lib/contributors/prompt.js b/lib/contributors/prompt.js index d82c8b5..07fb3cc 100644 --- a/lib/contributors/prompt.js +++ b/lib/contributors/prompt.js @@ -18,7 +18,7 @@ var contributionChoices = _.flow( }) ); -function getQuestions(options, username, contributions, allContributions) { +function getQuestions(options, username, contributions) { return [{ type: 'input', name: 'username', @@ -31,21 +31,34 @@ function getQuestions(options, username, contributions, allContributions) { when: !contributions, default: function (answers) { // default values for contributions when updating existing users - return allContributions + answers.username = answers.username || username; + return options.contributors .filter((entry) => entry.login.toLowerCase() === answers.username.toLowerCase()) .reduce((memo, entry) => memo.concat(entry.contributions), []); }, choices: contributionChoices(options), - validate: input => input.length ? true : 'Use space to select at least one contribution type.' + validate: function (input, answers) { + answers.username = answers.username || username; + var previousContributions = options.contributors + .filter((entry) => entry.login.toLowerCase() === answers.username.toLowerCase()) + .reduce((memo, entry) => memo.concat(entry.contributions), []); + + if (!input.length) { + return 'Use space to select at least one contribution type.' + } else if (_.isEqual(input, previousContributions)) { + return 'Nothing changed, use space to select contribution types.' + } + return true; + } }]; } -module.exports = function prompt(options, username, contributions, allContributions) { +module.exports = function prompt(options, username, contributions) { var defaults = { username: username, contributions: contributions && contributions.split(',') }; - var questions = getQuestions(options, username, contributions, allContributions); + var questions = getQuestions(options, username, contributions); return inquirer.prompt(questions) .then(_.assign(defaults)); }; From cf3ca62b9dc1b344e425b6e3c7d286d5f40f29e9 Mon Sep 17 00:00:00 2001 From: Chris Vickery Date: Tue, 7 Nov 2017 09:09:00 -0800 Subject: [PATCH 3/3] Formatting --- lib/contributors/prompt.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/contributors/prompt.js b/lib/contributors/prompt.js index 07fb3cc..b91fe74 100644 --- a/lib/contributors/prompt.js +++ b/lib/contributors/prompt.js @@ -34,19 +34,19 @@ function getQuestions(options, username, contributions) { answers.username = answers.username || username; return options.contributors .filter((entry) => entry.login.toLowerCase() === answers.username.toLowerCase()) - .reduce((memo, entry) => memo.concat(entry.contributions), []); + .reduce((allEntries, entry) => allEntries.concat(entry.contributions), []); }, choices: contributionChoices(options), validate: function (input, answers) { answers.username = answers.username || username; var previousContributions = options.contributors .filter((entry) => entry.login.toLowerCase() === answers.username.toLowerCase()) - .reduce((memo, entry) => memo.concat(entry.contributions), []); + .reduce((allEntries, entry) => allEntries.concat(entry.contributions), []); if (!input.length) { - return 'Use space to select at least one contribution type.' + return 'Use space to select at least one contribution type.'; } else if (_.isEqual(input, previousContributions)) { - return 'Nothing changed, use space to select contribution types.' + return 'Nothing changed, use space to select contribution types.'; } return true; }