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

51 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, cb) {
2016-03-06 23:20:24 +00:00
infoFetcher(username, function (error, userData) {
2016-03-02 22:45:23 +00:00
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);
2016-03-06 23:20:24 +00:00
};