forked from withastro/astro.build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-themes.mjs
75 lines (62 loc) · 1.59 KB
/
update-themes.mjs
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
import { promises as fs } from 'node:fs'
import path from 'node:path'
import glob from 'fast-glob'
import { parseRepoUrl, orgApi } from './github.mjs'
async function withStars(theme) {
if (theme.official) {
return theme
}
const { org, repo } = parseRepoUrl(theme.repoUrl.href) ?? {}
if (!org || !repo) {
return theme
}
const stars = await orgApi(org).repo(repo).fetchStars()
return {
...theme,
stars
}
}
async function loadTheme(pathname) {
const data = JSON.parse(await fs.readFile(pathname, 'utf-8'))
const slug = path.basename(pathname, '.json')
return {
...data,
slug,
image: {
src: data.image,
alt: data.description,
},
tags: data.tags || [],
repoUrl: {
href: data.repoUrl,
text: data.title,
},
npmUrl: data.npmUrl && {
href: data.npmUrl,
text: data.title,
},
demoUrl: data.demoUrl && {
href: data.demoUrl,
text: data.title,
},
}
}
async function loadThemes() {
const promises = glob.sync('src/data/themes/*.json')
.map(loadTheme)
return await Promise.all(promises)
}
async function main() {
// load all themes JSON from src/data
const data = await loadThemes()
const themes = await Promise.all(
data
.sort(() => 0.5 - Math.random()) // randomize the order
.map(withStars) // include GitHub stars
)
await fs.writeFile(
'src/data/themes.json',
JSON.stringify(themes, null, 4)
)
}
main()