/
update-sponsors.ts
91 lines (86 loc) · 2.46 KB
/
update-sponsors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fs from 'fs'
import axios from 'axios'
import enquirer from 'enquirer'
import replaceSection from 'replace-section'
async function main() {
const { token } = process.env.GITHUB_TOKEN
? { token: process.env.GITHUB_TOKEN }
: await enquirer.prompt<{ token: string }>([
{
type: 'input',
message: 'Paste GitHub Personal Access Token',
name: 'token',
required: true,
},
])
const { data } = await axios.post(
`https://api.github.com/graphql
`,
{
query: `query {
viewer {
login
sponsorshipsAsMaintainer(first: 100) {
totalCount
nodes {
tier {
monthlyPriceInDollars
}
sponsorEntity {
... on User {
login
avatarUrl
}
... on Organization {
login
avatarUrl
}
}
}
}
}
}`,
},
{
headers: {
Authorization: `bearer ${token}`,
},
},
)
const totalCount = data.data.viewer.sponsorshipsAsMaintainer.totalCount
const sponsors = data.data.viewer.sponsorshipsAsMaintainer.nodes
.map((node) => {
return {
monthlyPriceInDollars: node.tier.monthlyPriceInDollars,
username: node.sponsorEntity.login,
avatar: node.sponsorEntity.avatarUrl,
private: node.privacyLevel === 'PRIVATE',
}
})
.filter((node) => {
return node.monthlyPriceInDollars >= 10 && !node.private
})
.sort((a, b) => {
return a.monthlyPriceInDollars > b.monthlyPriceInDollars ? -1 : 1
})
const code = sponsors
.map((sponsor) => {
return `<a title="${sponsor.username}" href="https://github.com/${sponsor.username}"><img src="${sponsor.avatar}" width="60" alt="profile picture of ${sponsor.username}"></a>`
})
.join(' ')
const readme = await fs.promises.readFile('README.md', 'utf8')
await fs.promises.writeFile(
'README.md',
replaceSection({
input: readme,
startWith: '<!-- replace-sponsors -->',
endWith: '<!-- replace-sponsors -->',
replaceWith: `<!-- replace-sponsors -->
${code}
...[and ${totalCount - sponsors.length} more](https://egoist.dev/thanks)
<!-- replace-sponsors -->`,
}),
'utf-8',
)
}
main()