forked from OpenWebGAL/WebGAL_HomePage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-contributors.js
62 lines (52 loc) · 1.98 KB
/
update-contributors.js
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
const fs = require('fs')
const repos = [
'OpenWebGAL/WebGAL',
'OpenWebGAL/WebGAL_Terre',
'OpenWebGAL/WebGAL_Doc',
'OpenWebGAL/WebGAL_HomePage'
]
const contributorsList = repos.map(repo => `https://api.github.com/repos/${repo}/contributors`)
const getInfo = async (url) => fetch(url).then(res => res.json())
let allContributors = []
const filterContributors = (contributors) => contributors.map(contributor => {
if (contributor.login !== 'dependabot[bot]') {
const newContributor = {
login: contributor.login,
avatarUrl: contributor.avatar_url,
url: contributor.html_url,
contributions: contributor.contributions,
}
const test = allContributors.find(item => item.login === newContributor.login)
if (test)
allContributors = [...allContributors.filter(item => item.login !== test.login), { ...test, contributions: test.contributions + newContributor.contributions }]
else
allContributors = [...allContributors, newContributor]
}
})
const getContributors = async () => {
await Promise.all(contributorsList.map(url => getInfo(url).then(contributors => filterContributors(contributors))))
allContributors = await Promise.all(
[...allContributors]
.sort((a, b) => b.contributions - a.contributions)
.map(async item => {
const name = await getInfo(`https://api.github.com/users/${item.login}`).then(res => res.name)
return {
name: name ? name : item.login,
avatarUrl: item.avatarUrl,
url: item.url,
}
})
)
console.log(allContributors)
fs.readFile('./data/contributiors.ts', 'utf-8', (err, data) => {
if (err) throw err
else {
newFile = data.replace(/contributiors: Contributor\[\] = \[.*\]/s, `contributiors: Contributor[] = ${JSON.stringify(allContributors)}`).replaceAll('"', '\'')
fs.writeFile('./data/contributiors.ts', newFile, (err) => {
if (err) throw err
else console.log('Successfully!')
})
}
})
}
getContributors()