Add --commit option to commit on contributor add/update

This commit is contained in:
Jeroen Engels 2016-03-29 22:59:21 +02:00
parent 4b2fb1bcc7
commit d780c3c94f
5 changed files with 55 additions and 10 deletions

View file

@ -2,6 +2,7 @@
"projectOwner": "jfmengels" "projectOwner": "jfmengels"
, "projectName": "all-contributors-cli" , "projectName": "all-contributors-cli"
, "imageSize": 100 , "imageSize": 100
, "commit": true
, "contributors": [ , "contributors": [
{ {
"login": "jfmengels" "login": "jfmengels"

21
cli.js
View file

@ -8,7 +8,7 @@ var yargs = require('yargs');
var init = require('./lib/init'); var init = require('./lib/init');
var generate = require('./lib/generate'); var generate = require('./lib/generate');
var markdown = require('./lib/util').markdown; var util = require('./lib/util');
var updateContributors = require('./lib/contributors'); var updateContributors = require('./lib/contributors');
var cwd = process.cwd(); var cwd = process.cwd();
@ -23,6 +23,7 @@ var argv = yargs
.usage('Usage: $0 add <username> <contribution>') .usage('Usage: $0 add <username> <contribution>')
.command('init', 'Prepare the project to be used with this tool') .command('init', 'Prepare the project to be used with this tool')
.usage('Usage: $0 init') .usage('Usage: $0 init')
.boolean('commit')
.default('files', ['README.md']) .default('files', ['README.md'])
.default('contributorsPerLine', 7) .default('contributorsPerLine', 7)
.default('contributors', []) .default('contributors', [])
@ -45,12 +46,12 @@ function startGeneration(argv, cb) {
return path.join(cwd, file); return path.join(cwd, file);
}) })
.forEach(function (file) { .forEach(function (file) {
markdown.read(file, function (error, fileContent) { util.markdown.read(file, function (error, fileContent) {
if (error) { if (error) {
return cb(error); return cb(error);
} }
var newFileContent = generate(argv, argv.contributors, fileContent); var newFileContent = generate(argv, argv.contributors, fileContent);
markdown.write(file, newFileContent, cb); util.markdown.write(file, newFileContent, cb);
}); });
}); });
} }
@ -59,12 +60,20 @@ function addContribution(argv, cb) {
var username = argv._[1]; var username = argv._[1];
var contributions = argv._[2]; var contributions = argv._[2];
// Add or update contributor in the config file // Add or update contributor in the config file
updateContributors(argv, username, contributions, function (error, contributors) { updateContributors(argv, username, contributions, function (error, data) {
if (error) { if (error) {
return onError(error); return onError(error);
} }
argv.contributors = contributors; argv.contributors = data.contributors;
startGeneration(argv, cb); startGeneration(argv, function (error) {
if (error) {
return cb(error);
}
if (!argv.commit) {
return cb();
}
return util.git.commit(argv, data, cb);
});
}); });
} }

View file

@ -1,10 +1,15 @@
'use strict'; 'use strict';
var _ = require('lodash/fp');
var util = require('../util'); var util = require('../util');
var add = require('./add'); var add = require('./add');
var github = require('./github'); var github = require('./github');
var prompt = require('./prompt'); var prompt = require('./prompt');
function isNewContributor(contributorList, username) {
return !_.find({login: username}, contributorList);
}
module.exports = function addContributor(options, username, contributions, cb) { module.exports = function addContributor(options, username, contributions, cb) {
prompt(options, username, contributions, function (answers) { prompt(options, username, contributions, function (answers) {
add(options, answers.username, answers.contributions, github, function (error, contributors) { add(options, answers.username, answers.contributions, github, function (error, contributors) {
@ -12,7 +17,12 @@ module.exports = function addContributor(options, username, contributions, cb) {
return cb(error); return cb(error);
} }
util.configFile.writeContributors(options.config, contributors, function (error) { util.configFile.writeContributors(options.config, contributors, function (error) {
return cb(error, contributors); return cb(error, {
username: answers.username,
contributions: answers.contributions,
contributors: contributors,
newContributor: isNewContributor(options.contributors, answers.username)
});
}); });
}); });
}); });

View file

@ -2,7 +2,7 @@
var _ = require('lodash/fp'); var _ = require('lodash/fp');
var inquirer = require('inquirer'); var inquirer = require('inquirer');
var getRepoInfo = require('../util').git; var git = require('../util').git;
var questions = [{ var questions = [{
type: 'input', type: 'input',
@ -45,7 +45,7 @@ var uniqueFiles = _.flow(
); );
module.exports = function prompt(cb) { module.exports = function prompt(cb) {
getRepoInfo(function (error, repoInfo) { git.getRepoInfo(function (error, repoInfo) {
if (error) { if (error) {
return cb(error); return cb(error);
} }

View file

@ -1,5 +1,9 @@
'use strict'; 'use strict';
var _ = require('lodash/fp');
var commitTemplate = '<%= (newContributor ? "Add" : "Update") %> <%= username %> as a contributor';
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
function getRemoteOriginData(cb) { function getRemoteOriginData(cb) {
@ -23,11 +27,32 @@ function parse(originUrl) {
}; };
} }
module.exports = function getRepoInfo(cb) { function getRepoInfo(cb) {
getRemoteOriginData(function (error, originUrl) { getRemoteOriginData(function (error, originUrl) {
if (error) { if (error) {
return cb(error); return cb(error);
} }
return cb(null, parse(originUrl)); return cb(null, parse(originUrl));
}); });
}
function spawnCommand(args, cb) {
var git = spawn('git', args);
git.stderr.on('data', cb);
git.on('close', cb);
}
function commit(options, data, cb) {
spawnCommand(['add', '.'], function (error) {
if (error) {
return cb(error);
}
var commitMessage = _.template(options.commitTemplate || commitTemplate)(data);
spawnCommand(['commit', '-m', commitMessage], cb);
});
}
module.exports = {
commit: commit,
getRepoInfo: getRepoInfo
}; };