-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathupdate-data.js
More file actions
145 lines (136 loc) · 4.62 KB
/
update-data.js
File metadata and controls
145 lines (136 loc) · 4.62 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const fs = require('fs')
const fetch = require('node-fetch')
const tiers = require('../data/tiers.json')
/** Repositories where commits are being considered as contributions. */
const repos = [
"AssemblyScript/assemblyscript",
"AssemblyScript/examples",
"AssemblyScript/website"
]
/** Excluded GitHub user names (of bots). */
const githubExcludes = [
"dependabot[bot]",
"jsdelivrbot"
]
/** Inherited OSC user names (of duplicate accounts). */
const sponsorInherits = {
"nearprotocol": [ "nearprotocol1" ]
}
/** Excluded OSC user names (of defunct accounts). */
const sponsorExcludes = []
const defaultLogoSize = 32
/** Computes the size of a sponsor logo times two in case of higher pixel densities. */
function getLogoSize(item) {
const totalAmountDonated = item.totalAmountDonated
if (typeof totalAmountDonated === 'number') {
for (const tier of Object.values(tiers)) {
if (totalAmountDonated >= tier.minAmount) {
return 2 * tier.size
}
}
}
return 2 * defaultLogoSize
}
/** Updates sponsors data, also substituting custom-made logos. */
function updateSponsors() {
const files = fs.readdirSync(__dirname + '/../src/.vuepress/public/sponsors')
const logos = {}
files.forEach(file => {
if (/\.(svg|png)$/.test(file)) {
const id = file.substring(0, file.lastIndexOf('.'))
logos[id] = 'sponsors/' + file
}
})
fetch('https://opencollective.com/assemblyscript/members/all.json', {
headers: {
accept: 'application/json'
}
})
.then(async res => {
const text = await res.text()
try {
return JSON.parse(text)
} catch (err) {
throw new Error('Invalid OpenCollective response')
}
})
.then(json => {
const seen = new Map()
const sponsors = json
.map(item => {
item.username = item.profile.substring(item.profile.lastIndexOf('/') + 1)
return item
})
.filter(item => {
if (seen.has(item.username)) return false
seen.set(item.username, item.totalAmountDonated)
return item.isActive && item.totalAmountDonated > 0
})
.map(item => {
const inherits = sponsorInherits[item.username]
if (Array.isArray(inherits)) {
for (let othername of inherits) {
json
.filter(otheritem => otheritem.username == othername)
.forEach(otheritem => item.totalAmountDonated += otheritem.totalAmountDonated)
seen.delete(othername)
}
}
return item
})
.filter(item => !sponsorExcludes.includes(item.username) && seen.has(item.username))
.sort((a, b) => b.totalAmountDonated - a.totalAmountDonated)
.map(item => {
const logoSize = getLogoSize(item)
const logo = logos[item.username] || 'https://images.opencollective.com/' + item.username + '/avatar/' + logoSize + '.jpg'
const link = item.totalAmountDonated >= tiers.bronze.minAmount
? item.website || item.profile
: item.profile
return {
// id: item.MemberId,
name: item.name,
logo: logo,
link: link,
amount: item.totalAmountDonated
}
})
fs.writeFileSync(__dirname + '/../data/sponsors.json', JSON.stringify(sponsors, null, 2))
})
.catch(err => {
console.warn('Failed to update sponsors, keeping cached data')
console.warn(err.message)
})
}
/** Updates contributors data by pulling stats from GitHub. */
function updateContributors() {
const contributors = {}
Promise.all(repos.map(repo => fetch('http://api.github.com/repos/' + repo + '/contributors').then(res => res.json())))
.then(jsons => {
jsons.forEach(json => {
json
.filter(item => !githubExcludes.includes(item.login))
.forEach(item => {
const contributor = contributors[item.id]
if (contributor) {
contributor.count += item.contributions
} else {
contributors[item.id] = {
// id: item.id,
name: item.login,
logo: item.avatar_url + '&s=' + getLogoSize(item),
link: item.html_url,
count: item.contributions
}
}
})
})
const sortedContributors = Object.values(contributors).sort((a, b) => b.count - a.count)
fs.writeFileSync(__dirname + '/../data/contributors.json', JSON.stringify(sortedContributors, null, 2))
})
.catch(err => {
console.error(err.stack)
process.exit(1)
})
}
updateSponsors()
updateContributors()