all-contributors-cli/lib/contributors/add.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-03-02 22:45:23 +00:00
'use strict';
var _ = require('lodash/fp');
function uniqueTypes(contribution) {
return contribution.type || contribution;
}
function formatContributions(options, existing, types) {
2016-03-02 22:45:23 +00:00
if (options.url) {
2016-03-06 23:20:24 +00:00
return (existing || []).concat(types.map(function (type) {
return {type: type, url: options.url};
2016-03-02 22:45:23 +00:00
}));
}
2016-03-06 23:20:24 +00:00
return _.uniqBy(uniqueTypes, (existing || []).concat(types));
2016-03-02 22:45:23 +00:00
}
function updateContributor(options, contributor, contributions) {
return _.assign(contributor, {
contributions: formatContributions(options, contributor.contributions, contributions)
});
}
function updateExistingContributor(options, username, contributions) {
2016-03-06 23:20:24 +00:00
return options.contributors.map(function (contributor) {
2016-03-02 22:45:23 +00:00
if (username !== contributor.login) {
return contributor;
}
return updateContributor(options, contributor, contributions);
});
}
function addNewContributor(options, username, contributions, infoFetcher) {
return infoFetcher(username)
.then(userData => {
2016-03-02 22:45:23 +00:00
var contributor = _.assign(userData, {
contributions: formatContributions(options, [], contributions)
});
return options.contributors.concat(contributor);
2016-03-02 22:45:23 +00:00
});
}
module.exports = function addContributor(options, username, contributions, infoFetcher) {
2016-03-02 22:45:23 +00:00
if (_.find({login: username}, options.contributors)) {
return Promise.resolve(updateExistingContributor(options, username, contributions));
2016-03-02 22:45:23 +00:00
}
return addNewContributor(options, username, contributions, infoFetcher);
2016-03-06 23:20:24 +00:00
};