forked from chakra-ui/chakra-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatars.ts
100 lines (84 loc) · 2.5 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
import path from "path"
import { promises as fs } from "fs"
import mkdirp from "mkdirp"
import fetch from "node-fetch"
import tweetsJson from "../website/configs/tweets.json"
import ImageCache from "./image-cache"
const { tweets } = tweetsJson
const websiteDir = path.join(__dirname, "..", "website")
const publicDir = path.join(websiteDir, "public")
const avatarsDir = path.join(publicDir, "avatars")
/**
* 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 sponsors = await response.json()
const individuals = sponsors.filter(
(i: any) => i.type === "USER" && i.image != null,
)
const companies = sponsors.filter((i: any) => i.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: any) => {
const filename = await individualAvatarsCache.urlToFile(
individual.image,
individual.MemberId,
)
return {
...individual,
image: `/avatars/${filename}`,
}
}),
)
const companyAvatarsCache = new ImageCache({
outputDirectory: avatarsDir,
})
const companySponsors = await Promise.all(
companies.map(async (company: any) => {
const filename = await companyAvatarsCache.urlToFile(
company.image,
company.MemberId,
)
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: any) => {
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)
}