@bevry/update-contributors
Version:
Update the package.json author, contributors, and maintainers fields with the latest remote data
64 lines (63 loc) • 2.76 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const github_contributors_1 = require("@bevry/github-contributors");
const json_1 = require("@bevry/json");
const util_js_1 = require("./util.js");
async function updateContributors(path) {
let localCount = 0, remoteCount = 0;
// read
const pkg = await (0, json_1.readJSON)(path);
// slug
const githubRepoSlug = (0, util_js_1.getGitHubRepoSlug)(pkg);
const slug = githubRepoSlug || pkg.name;
if (!slug) {
console.error(path, pkg);
throw new Error('package needs at least a name to identify it uniquely');
}
// Add local people to the singleton with their appropriate permissions
github_contributors_1.Fellow.add(pkg.author, pkg.authors).forEach((person) => {
person.authoredRepositories.add(slug);
});
github_contributors_1.Fellow.add(pkg.contributors).forEach((person) => {
person.contributedRepositories.add(slug);
});
github_contributors_1.Fellow.add(pkg.maintainers).forEach((person) => {
person.maintainedRepositories.add(slug);
});
localCount = github_contributors_1.Fellow.fellows.length;
// Enhance authors, contributors and maintainers with latest remote data
if (githubRepoSlug) {
try {
const added = await (0, github_contributors_1.getContributorsFromRepoContributorData)(githubRepoSlug);
remoteCount = added.size;
}
catch (err) {
console.warn(err);
console.warn(`FAILED to fetch the remote contributors for the repository: ${githubRepoSlug}`);
}
}
// update the data with the converged data
delete pkg.authors;
pkg.author = github_contributors_1.Fellow.authorsRepository(slug)
.map((fellow) => fellow.toString({ displayYears: true, displayEmail: true }))
.join(', ');
pkg.contributors = github_contributors_1.Fellow.contributesRepository(slug)
.map((fellow) => fellow.toString({ displayEmail: true, urlFields: ['githubUrl', 'url'] }))
.filter((entry) => entry.includes('[bot]') === false)
.sort();
pkg.maintainers = github_contributors_1.Fellow.maintainsRepository(slug)
.map((fellow) => fellow.toString({ displayEmail: true, urlFields: ['githubUrl', 'url'] }))
.sort();
// clean up in case empty
if (!pkg.author)
delete pkg.author;
if (pkg.contributors.length === 0)
delete pkg.contributors;
if (pkg.maintainers.length === 0)
delete pkg.maintainers;
// write it
await (0, json_1.writeJSON)(path, pkg);
// done
console.log(`Updated contributors (${localCount} local, ${remoteCount} remote) on [${path}]`);
}
exports.default = updateContributors;
;