UNPKG

2.74 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const github_contributors_1 = require("@bevry/github-contributors");
4const json_1 = require("@bevry/json");
5const util_js_1 = require("./util.js");
6async function updateContributors(path) {
7 let localCount = 0, remoteCount = 0;
8 // read
9 const pkg = await json_1.readJSON(path);
10 // slug
11 const githubRepoSlug = util_js_1.getGitHubRepoSlug(pkg);
12 const slug = githubRepoSlug || pkg.name;
13 if (!slug) {
14 console.error(path, pkg);
15 throw new Error('package needs at least a name to identify it uniquely');
16 }
17 // Add local people to the singleton with their appropriate permissions
18 github_contributors_1.Fellow.add(pkg.author, pkg.authors).forEach((person) => {
19 person.authoredRepositories.add(slug);
20 });
21 github_contributors_1.Fellow.add(pkg.contributors).forEach((person) => {
22 person.contributedRepositories.add(slug);
23 });
24 github_contributors_1.Fellow.add(pkg.maintainers).forEach((person) => {
25 person.maintainedRepositories.add(slug);
26 });
27 localCount = github_contributors_1.Fellow.fellows.length;
28 // Enhance authors, contributors and maintainers with latest remote data
29 if (githubRepoSlug) {
30 try {
31 const added = await github_contributors_1.getContributorsFromRepoContributorData(githubRepoSlug);
32 remoteCount = added.size;
33 }
34 catch (err) {
35 console.warn(err);
36 console.warn(`FAILED to fetch the remote contributors for the repository: ${githubRepoSlug}`);
37 }
38 }
39 // update the data with the converged data
40 delete pkg.authors;
41 pkg.author = github_contributors_1.Fellow.authorsRepository(slug)
42 .map((fellow) => fellow.toString({ displayYears: true, displayEmail: true }))
43 .join(', ');
44 pkg.contributors = github_contributors_1.Fellow.contributesRepository(slug)
45 .map((fellow) => fellow.toString({ displayEmail: true, urlFields: ['githubUrl', 'url'] }))
46 .filter((entry) => entry.includes('[bot]') === false)
47 .sort();
48 pkg.maintainers = github_contributors_1.Fellow.maintainsRepository(slug)
49 .map((fellow) => fellow.toString({ displayEmail: true, urlFields: ['githubUrl', 'url'] }))
50 .sort();
51 // clean up in case empty
52 if (!pkg.author)
53 delete pkg.author;
54 if (pkg.contributors.length === 0)
55 delete pkg.contributors;
56 if (pkg.maintainers.length === 0)
57 delete pkg.maintainers;
58 // write it
59 await json_1.writeJSON(path, pkg);
60 // done
61 console.log(`Updated contributors (${localCount} local, ${remoteCount} remote) on [${path}]`);
62}
63exports.default = updateContributors;