diff --git a/.all-contributorsrc b/.all-contributorsrc index 5287317..2fcf444 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -2,6 +2,7 @@ "projectOwner": "jfmengels" , "projectName": "all-contributors-cli" , "imageSize": 100 +, "commit": true , "contributors": [ { "login": "jfmengels" diff --git a/cli.js b/cli.js index e02cdb2..581bae3 100755 --- a/cli.js +++ b/cli.js @@ -8,7 +8,7 @@ var yargs = require('yargs'); var init = require('./lib/init'); var generate = require('./lib/generate'); -var markdown = require('./lib/util').markdown; +var util = require('./lib/util'); var updateContributors = require('./lib/contributors'); var cwd = process.cwd(); @@ -23,6 +23,7 @@ var argv = yargs .usage('Usage: $0 add ') .command('init', 'Prepare the project to be used with this tool') .usage('Usage: $0 init') + .boolean('commit') .default('files', ['README.md']) .default('contributorsPerLine', 7) .default('contributors', []) @@ -45,12 +46,12 @@ function startGeneration(argv, cb) { return path.join(cwd, file); }) .forEach(function (file) { - markdown.read(file, function (error, fileContent) { + util.markdown.read(file, function (error, fileContent) { if (error) { return cb(error); } 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 contributions = argv._[2]; // Add or update contributor in the config file - updateContributors(argv, username, contributions, function (error, contributors) { + updateContributors(argv, username, contributions, function (error, data) { if (error) { return onError(error); } - argv.contributors = contributors; - startGeneration(argv, cb); + argv.contributors = data.contributors; + startGeneration(argv, function (error) { + if (error) { + return cb(error); + } + if (!argv.commit) { + return cb(); + } + return util.git.commit(argv, data, cb); + }); }); } diff --git a/lib/contributors/index.js b/lib/contributors/index.js index cddf8e1..4f0377a 100644 --- a/lib/contributors/index.js +++ b/lib/contributors/index.js @@ -1,10 +1,15 @@ 'use strict'; +var _ = require('lodash/fp'); var util = require('../util'); var add = require('./add'); var github = require('./github'); var prompt = require('./prompt'); +function isNewContributor(contributorList, username) { + return !_.find({login: username}, contributorList); +} + module.exports = function addContributor(options, username, contributions, cb) { prompt(options, username, contributions, function (answers) { 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); } 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) + }); }); }); }); diff --git a/lib/init/prompt.js b/lib/init/prompt.js index 7ba0ecd..2a051fa 100644 --- a/lib/init/prompt.js +++ b/lib/init/prompt.js @@ -2,7 +2,7 @@ var _ = require('lodash/fp'); var inquirer = require('inquirer'); -var getRepoInfo = require('../util').git; +var git = require('../util').git; var questions = [{ type: 'input', @@ -45,7 +45,7 @@ var uniqueFiles = _.flow( ); module.exports = function prompt(cb) { - getRepoInfo(function (error, repoInfo) { + git.getRepoInfo(function (error, repoInfo) { if (error) { return cb(error); } diff --git a/lib/util/git.js b/lib/util/git.js index f83cfde..5d86260 100644 --- a/lib/util/git.js +++ b/lib/util/git.js @@ -1,5 +1,9 @@ 'use strict'; +var _ = require('lodash/fp'); + +var commitTemplate = '<%= (newContributor ? "Add" : "Update") %> <%= username %> as a contributor'; + var spawn = require('child_process').spawn; function getRemoteOriginData(cb) { @@ -23,11 +27,32 @@ function parse(originUrl) { }; } -module.exports = function getRepoInfo(cb) { +function getRepoInfo(cb) { getRemoteOriginData(function (error, originUrl) { if (error) { return cb(error); } 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 };