mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-10 05:56:29 +00:00
57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
'use strict';
|
||
|
||
var _ = require('lodash/fp');
|
||
|
||
function matchContribution(type) {
|
||
return function(existing) {
|
||
return type === existing || type === existing.type;
|
||
};
|
||
}
|
||
|
||
function uniqueTypes(contribution) {
|
||
return contribution.type || contribution;
|
||
}
|
||
|
||
function formatContributions(options, existing, newTypes) {
|
||
var types = newTypes.split(',');
|
||
if (options.url) {
|
||
return (existing || []).concat(types.map(function(type) {
|
||
return { type: type, url: options.url };
|
||
}));
|
||
}
|
||
return _.uniqBy(uniqueTypes, (existing || []).concat(types));
|
||
}
|
||
|
||
function updateContributor(options, contributor, contributions) {
|
||
return _.assign(contributor, {
|
||
contributions: formatContributions(options, contributor.contributions, contributions)
|
||
});
|
||
}
|
||
|
||
function updateExistingContributor(options, username, contributions) {
|
||
return options.contributors.map(function(contributor, index) {
|
||
if (username !== contributor.login) {
|
||
return contributor;
|
||
}
|
||
return updateContributor(options, contributor, contributions);
|
||
});
|
||
}
|
||
|
||
function addNewContributor(options, username, contributions, infoFetcher, cb) {
|
||
infoFetcher(username, function(error, userData) {
|
||
if (error) {
|
||
return cb(error);
|
||
}
|
||
var contributor = _.assign(userData, {
|
||
contributions: formatContributions(options, [], contributions)
|
||
});
|
||
return cb(null, options.contributors.concat(contributor));
|
||
});
|
||
}
|
||
|
||
module.exports = function addContributor(options, username, contributions, infoFetcher, cb) {
|
||
if (_.find({login: username}, options.contributors)) {
|
||
return cb(null, updateExistingContributor(options, username, contributions));
|
||
}
|
||
return addNewContributor(options, username, contributions, infoFetcher, cb);
|
||
}
|