all-contributors-cli/lib/util/git.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
2016-03-30 18:11:37 +00:00
var path = require('path');
2016-05-05 14:27:26 +00:00
var spawn = require('child_process').spawn;
var _ = require('lodash/fp');
var commitTemplate = '<%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor';
function getRemoteOriginData(cb) {
var output = '';
var git = spawn('git', 'config --get remote.origin.url'.split(' '));
git.stdout.on('data', function (data) {
output += data;
});
git.stderr.on('data', cb);
git.on('close', function () {
cb(null, output);
});
}
function parse(originUrl) {
2016-05-05 14:27:26 +00:00
var result = /:(\w+)\/([A-Za-z0-9-_]+)/.exec(originUrl);
if (!result) {
return null;
}
return {
projectOwner: result[1],
projectName: result[2]
};
}
function getRepoInfo(cb) {
getRemoteOriginData(function (error, originUrl) {
if (error) {
return cb(error);
}
return cb(null, parse(originUrl));
});
}
2016-03-30 18:11:37 +00:00
function spawnGitCommand(args, cb) {
var git = spawn('git', args);
git.stderr.on('data', cb);
git.on('close', cb);
}
function commit(options, data, cb) {
2016-03-30 18:11:37 +00:00
var files = options.files.concat(options.config);
var absolutePathFiles = files.map(function (file) {
return path.resolve(process.cwd(), file);
});
spawnGitCommand(['add'].concat(absolutePathFiles), function (error) {
if (error) {
return cb(error);
}
var commitMessage = _.template(options.commitTemplate || commitTemplate)(data);
2016-03-30 18:11:37 +00:00
spawnGitCommand(['commit', '-m', commitMessage], cb);
});
}
module.exports = {
commit: commit,
getRepoInfo: getRepoInfo
};