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