2016-03-21 23:41:24 +00:00
|
|
|
'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;
|
2016-03-29 20:59:21 +00:00
|
|
|
var _ = require('lodash/fp');
|
2017-02-15 21:25:32 +00:00
|
|
|
var pify = require('pify');
|
2016-03-29 20:59:21 +00:00
|
|
|
|
2016-05-11 18:38:20 +00:00
|
|
|
var commitTemplate = '<%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor';
|
2016-03-29 20:59:21 +00:00
|
|
|
|
2017-02-15 21:25:32 +00:00
|
|
|
var getRemoteOriginData = pify(cb => {
|
2016-03-21 23:41:24 +00:00
|
|
|
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);
|
|
|
|
});
|
2017-02-15 21:25:32 +00:00
|
|
|
});
|
2016-03-21 23:41:24 +00:00
|
|
|
|
|
|
|
function parse(originUrl) {
|
2016-05-05 14:27:26 +00:00
|
|
|
var result = /:(\w+)\/([A-Za-z0-9-_]+)/.exec(originUrl);
|
2016-05-10 13:39:44 +00:00
|
|
|
if (!result) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:41:24 +00:00
|
|
|
return {
|
|
|
|
projectOwner: result[1],
|
|
|
|
projectName: result[2]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-15 21:25:32 +00:00
|
|
|
function getRepoInfo() {
|
|
|
|
return getRemoteOriginData()
|
|
|
|
.then(parse);
|
2016-03-29 20:59:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 21:25:32 +00:00
|
|
|
var spawnGitCommand = pify((args, cb) => {
|
2016-03-29 20:59:21 +00:00
|
|
|
var git = spawn('git', args);
|
|
|
|
git.stderr.on('data', cb);
|
|
|
|
git.on('close', cb);
|
2017-02-15 21:25:32 +00:00
|
|
|
});
|
2016-03-29 20:59:21 +00:00
|
|
|
|
2017-02-15 21:25:32 +00:00
|
|
|
function commit(options, data) {
|
2016-03-30 18:11:37 +00:00
|
|
|
var files = options.files.concat(options.config);
|
2017-02-15 21:25:32 +00:00
|
|
|
var absolutePathFiles = files.map(file => {
|
2016-03-30 18:11:37 +00:00
|
|
|
return path.resolve(process.cwd(), file);
|
|
|
|
});
|
2017-02-15 21:25:32 +00:00
|
|
|
return spawnGitCommand(['add'].concat(absolutePathFiles))
|
|
|
|
.then(() => {
|
|
|
|
var commitMessage = _.template(options.commitTemplate || commitTemplate)(data);
|
|
|
|
return spawnGitCommand(['commit', '-m', commitMessage]);
|
|
|
|
});
|
2016-03-29 20:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
commit: commit,
|
|
|
|
getRepoInfo: getRepoInfo
|
2016-03-21 23:41:24 +00:00
|
|
|
};
|