diff --git a/cli.js b/cli.js index afa4eb4..6db9fb4 100755 --- a/cli.js +++ b/cli.js @@ -5,6 +5,7 @@ var fs = require('fs'); var path = require('path'); +var init = require('./lib/init'); var generate = require('./lib/generate'); var markdown = require('./lib/markdown'); var updateContributors = require('./lib/contributors'); @@ -19,6 +20,8 @@ var argv = require('yargs') .usage('Usage: $0 generate') .command('add', 'add a new contributor') .usage('Usage: $0 add ') + .command('init', 'Prepare the project to be used with this tool') + .usage('Usage: $0 init') .demand(2) .default('files', ['README.md']) .default('contributorsPerLine', 7) @@ -60,7 +63,9 @@ function onError(error) { var command = argv._[0]; -if (command === 'generate') { +if (command === 'init') { + init(onError); +} else if (command === 'generate') { startGeneration(argv, onError); } else if (command === 'add') { var username = argv._[1]; diff --git a/lib/configFile.js b/lib/configFile.js index 67da1fc..fbe2265 100644 --- a/lib/configFile.js +++ b/lib/configFile.js @@ -17,6 +17,10 @@ function readConfig(configPath) { return JSON.parse(fs.readFileSync(configPath, 'utf-8')); } +function writeConfig(configPath, content, cb) { + return fs.writeFile(configPath, formatCommaFirst(content), cb); +} + function writeContributors(configPath, contributors, cb) { var config = readConfig(configPath); var content = _.assign(config, {contributors: contributors}); @@ -25,5 +29,6 @@ function writeContributors(configPath, contributors, cb) { module.exports = { readConfig: readConfig, + writeConfig: writeConfig, writeContributors: writeContributors }; diff --git a/lib/init/index.js b/lib/init/index.js new file mode 100644 index 0000000..3e7bd86 --- /dev/null +++ b/lib/init/index.js @@ -0,0 +1,10 @@ +'use strict'; + +var prompt = require('./prompt'); +var configFile = require('../configFile'); + +module.exports = function init(cb) { + prompt(function postPrompt(result) { + configFile.writeConfig('.all-contributorsrc', result.config, cb); + }); +}; diff --git a/lib/init/prompt.js b/lib/init/prompt.js new file mode 100644 index 0000000..a1079cf --- /dev/null +++ b/lib/init/prompt.js @@ -0,0 +1,61 @@ +'use strict'; + +var _ = require('lodash/fp'); +var inquirer = require('inquirer'); + +var questions = [{ + type: 'input', + name: 'projectName', + message: "What's the name of the repository?" +}, { + type: 'input', + name: 'projectOwner', + message: "Who is the owner of the repository?" +}, { + type: 'input', + name: 'contributorFile', + message: 'In which file should contributors be listed?', + default: 'README.md' +}, { + type: 'confirm', + name: 'needBadge', + message: "Do you want a badge tallying the number of contributors?" +}, { + type: 'input', + name: 'badgeFile', + message: "In which file should the badge be shown?", + when: function (answers) { + return answers.needBadge; + }, + default: function (answers) { + return answers.contributorFile; + } +}, { + type: 'input', + name: 'imageSize', + message: "How big should the avatars be? (in px)", + filter: parseInt, + default: 100 +}]; + +var uniqueFiles = _.flow( + _.compact, + _.uniq +); + +module.exports = function prompt(cb) { + inquirer.prompt(questions, function treatAnswers(answers) { + var config = { + projectName: answers.projectName, + projectOwner: answers.projectOwner, + files: uniqueFiles([answers.contributorFile, answers.badgeFile]), + imageSize: answers.imageSize, + contributors: [] + }; + return cb({ + config: config, + contributorFile: answers.contributorFile, + badgeFile: answers.badgeFile + }); + }); +}; diff --git a/package.json b/package.json index 97b7b19..f4df20c 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "homepage": "https://github.com/jfmengels/all-contributors-cli#readme", "dependencies": { + "inquirer": "^0.12.0", "lodash": "^4.6.1", "request": "^2.69.0", "yargs": "^4.3.2"