forked from chakra-ui/chakra-ui-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatars.ts
130 lines (112 loc) · 3.18 KB
/
avatars.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
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
import { promises as fs } from 'fs'
import mkdirp from 'mkdirp'
import fetch from 'node-fetch'
import path from 'path'
import tweetsJson from '../configs/tweets.json'
import ImageCache from './image-cache'
const { tweets } = tweetsJson
const publicDir = path.join(process.cwd(), 'public')
const avatarsDir = path.join(publicDir, 'avatars')
interface Sponsor {
MemberId: number
createdAt: string
type: string
role: string
tier: string
isActive: boolean
totalAmountDonated: number
currency: string
lastTransactionAt: string
lastTransactionAmount: number
profile: string
name: string
company: string | null
description: string | null
image: string
email: string | null
twitter: string | null
github: string | null
website: string | null
}
/**
* Get all the Open Collective sponsors and
* arrange them into "individuals" and "organizations"
*/
async function getSponsors() {
const response = await fetch(
'https://opencollective.com/chakra-ui/members/all.json',
)
const unfilteredSponsors: Sponsor[] = await response.json()
// filter the sponsors by opencollectice profile link to avoid double entries
const sponsors = unfilteredSponsors.filter(
(currentSponsor, index, allSponsors) =>
index ===
allSponsors.findIndex((s) => s.profile === currentSponsor.profile),
)
const individuals = sponsors.filter(
(sponsor) => sponsor.type === 'USER' && sponsor.image != null,
)
const companies = sponsors.filter(
(sponsor) => sponsor.type === 'ORGANIZATION',
)
return { individuals, companies }
}
async function buildSponsors() {
const { individuals, companies } = await getSponsors()
// cache individual sponsor image and resize to `40px` width
const individualAvatarsCache = new ImageCache({
outputDirectory: avatarsDir,
width: 40,
})
// update the image property from open-collective to use the cached image
const individualSponsors = await Promise.all(
individuals.map(async (individual) => {
const filename = await individualAvatarsCache.urlToFile(
individual.image,
individual.MemberId.toString(),
)
return {
...individual,
image: `/avatars/${filename}`,
}
}),
)
const companyAvatarsCache = new ImageCache({
outputDirectory: avatarsDir,
})
const companySponsors = await Promise.all(
companies.map(async (company) => {
const filename = await companyAvatarsCache.urlToFile(
company.image,
company.MemberId.toString(),
)
return {
...company,
image: `/avatars/${filename}`,
}
}),
)
const data = { individuals: individualSponsors, companies: companySponsors }
await fs.writeFile('.all-sponsorsrc', JSON.stringify(data, null, 2))
}
async function buildTweets() {
const cache = new ImageCache({
outputDirectory: avatarsDir,
width: 50,
})
await Promise.all(
tweets.map(async (tweet) => {
await cache.urlToFile(tweet.image, tweet.handle)
}),
)
}
async function buildAvatars() {
// make sure the avatars directory exists
await mkdirp(avatarsDir)
await Promise.all([buildSponsors(), buildTweets()])
}
try {
buildAvatars()
} catch (err) {
console.log(err)
}