Add init command to generate rc file

This commit is contained in:
Jeroen Engels 2016-03-21 22:40:32 +01:00
parent dafef7536a
commit a33ef2307f
5 changed files with 83 additions and 1 deletions

7
cli.js
View file

@ -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 <username> <contribution>')
.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];

View file

@ -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
};

10
lib/init/index.js Normal file
View file

@ -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);
});
};

61
lib/init/prompt.js Normal file
View file

@ -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
});
});
};

View file

@ -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"