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