-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathfetch-avatars.ts
35 lines (30 loc) · 958 Bytes
/
fetch-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
import { join, resolve } from 'pathe'
import fs from 'fs-extra'
import { ofetch } from 'ofetch'
const docsDir = resolve(__dirname, '../')
const pathContributors = resolve(docsDir, 'contributors/contributors.json')
const dirAvatars = resolve(docsDir, 'public/user-avatars/')
let contributors: string[] = []
async function download(url: string, fileName: string) {
try {
console.log('downloading', fileName)
const image = await ofetch(url, { responseType: 'arrayBuffer' })
await fs.writeFile(fileName, Buffer.from(image))
} catch (e) {
console.log('error downloading', fileName)
}
}
export async function fetchAvatars() {
await fs.ensureDir(dirAvatars)
contributors = JSON.parse(
await fs.readFile(pathContributors, { encoding: 'utf-8' }),
)
await Promise.all(
contributors.map((name) =>
download(
`https://github.com/${name}.png?size=48`,
join(dirAvatars, `${name}.png`),
),
),
)
}