2016-02-29 10:45:19 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-02-29 22:32:46 +00:00
|
|
|
|
var uniq = require('lodash.uniq');
|
|
|
|
|
var values = require('lodash.values');
|
2016-02-29 10:45:19 +00:00
|
|
|
|
var template = require('lodash.template');
|
|
|
|
|
var findIndex = require('lodash.findindex');
|
|
|
|
|
|
|
|
|
|
function listAllContributors(start, lines) {
|
|
|
|
|
var i = 0;
|
|
|
|
|
while (start + i < lines.length && lines[start + i].indexOf('[![') === 0) {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return lines.slice(start, start + i);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 21:08:23 +00:00
|
|
|
|
var defaultTemplate =
|
2016-02-29 10:45:19 +00:00
|
|
|
|
'[![<%= user.name %>](<%= user.avatar_url %>&s=<%= options.imageSize %>)' +
|
|
|
|
|
'<br /><%= user.name %>](<%= user.html_url %>)' +
|
2016-02-29 21:08:23 +00:00
|
|
|
|
' | [<%= contributions %>](https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= user.login %>)';
|
2016-02-29 10:45:19 +00:00
|
|
|
|
|
2016-02-29 22:32:46 +00:00
|
|
|
|
function contributorEntry(options, user, existingContributions) {
|
|
|
|
|
var contributions = uniq((existingContributions || []).concat(
|
|
|
|
|
options.contributions
|
|
|
|
|
.map(function(contribution) {
|
|
|
|
|
return options.emoji[contribution];
|
|
|
|
|
})
|
|
|
|
|
)).join('');
|
2016-02-29 10:45:19 +00:00
|
|
|
|
|
2016-02-29 21:08:23 +00:00
|
|
|
|
var contributionTemplate = template(options.template || defaultTemplate);
|
|
|
|
|
|
2016-02-29 10:45:19 +00:00
|
|
|
|
return contributionTemplate({
|
|
|
|
|
user: user,
|
|
|
|
|
contributions: contributions,
|
|
|
|
|
options: options
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 22:32:46 +00:00
|
|
|
|
function parseContributionTypes(options, line) {
|
|
|
|
|
return values(options.emoji)
|
|
|
|
|
.filter(function findExistingContribution(type) {
|
|
|
|
|
return line.indexOf(type) !== -1;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function upsertContributor(options, user, existingContributors) {
|
2016-02-29 10:45:19 +00:00
|
|
|
|
var contributor = contributorEntry(options, user);
|
2016-02-29 22:32:46 +00:00
|
|
|
|
var existingContributorIndex = findIndex(existingContributors, function(cont) {
|
|
|
|
|
return cont.indexOf(user.login) !== 1 && cont.indexOf(user.name) !== -1;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingContributorIndex === -1) {
|
|
|
|
|
return [].concat(existingContributors, contributor);
|
|
|
|
|
}
|
2016-02-29 10:45:19 +00:00
|
|
|
|
|
2016-02-29 22:32:46 +00:00
|
|
|
|
var contributionTypes = parseContributionTypes(options, existingContributors[existingContributorIndex]);
|
|
|
|
|
return [].concat(
|
|
|
|
|
existingContributors.slice(0, existingContributorIndex),
|
|
|
|
|
contributorEntry(options, user, contributionTypes),
|
|
|
|
|
existingContributors.slice(existingContributorIndex + 1)
|
|
|
|
|
).join('\n')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = function addContributor(options, user, fileContent) {
|
2016-02-29 10:45:19 +00:00
|
|
|
|
var lines = fileContent.split('\n');
|
|
|
|
|
var contributorListStart = findIndex(lines, function(line) {
|
|
|
|
|
return line.indexOf(':---: | :---:') !== -1;
|
|
|
|
|
});
|
2016-02-29 22:32:46 +00:00
|
|
|
|
|
|
|
|
|
var existingContributors = listAllContributors(contributorListStart + 1, lines);
|
|
|
|
|
var contributors = upsertContributor(options, user, existingContributors);
|
2016-02-29 10:45:19 +00:00
|
|
|
|
|
|
|
|
|
return [].concat(
|
|
|
|
|
lines.slice(0, contributorListStart + 1),
|
|
|
|
|
contributors,
|
2016-02-29 22:32:46 +00:00
|
|
|
|
lines.slice(contributorListStart + existingContributors.length + 1)
|
2016-02-29 10:45:19 +00:00
|
|
|
|
).join('\n')
|
|
|
|
|
}
|