-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
render.js
72 lines (56 loc) · 1.57 KB
/
render.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
63
64
65
66
67
68
69
70
71
72
const {dirname} = require('path')
const fs = require('mz/fs')
const {fetch} = require('./api')
const {createSvg} = require('./svg')
const total = new Map()
const query = `
query($owner: String!, $name: String!, $endCursor: String) {
repository(owner: $owner, name: $name) {
stargazers(first: 100, after: $endCursor) {
totalCount
edges {
starredAt
}
pageInfo {
hasNextPage
endCursor
}
}
}
rateLimit {
remaining
}
}
`
let rateLimit = {
remaining: 5000
}
async function render(path, owner, name) {
total.set(path, 0)
const data = await fetch(query, {owner, name})
if (data.repository) {
let {
stargazers: {
totalCount,
edges: dates,
pageInfo: {hasNextPage, endCursor}
}
} = data.repository
while (hasNextPage) {
total.set(path, Math.round(100 * dates.length / totalCount))
console.log(`${owner}/${name}: ${total.get(path)}%`)
const data = await fetch(query, {owner, name, endCursor})
hasNextPage = data.repository.stargazers.pageInfo.hasNextPage
endCursor = data.repository.stargazers.pageInfo.endCursor
rateLimit.remaining = data.rateLimit.remaining
dates = dates.concat(data.repository.stargazers.edges)
}
dates = dates.map(({starredAt}) => +(new Date(starredAt)))
const svg = createSvg(dates)
const dir = dirname(path)
await fs.exists(dir) || await fs.mkdir(dir)
fs.writeFile(path, svg)
}
total.delete(path)
}
module.exports = {render, total, rateLimit}