forked from middleapi/orpc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync-sponsor.ts
More file actions
271 lines (214 loc) · 7.89 KB
/
sync-sponsor.ts
File metadata and controls
271 lines (214 loc) · 7.89 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
interface Sponsor {
name: string | null
login: string
avatar: string
amount: number
createdAt: string
tierTitle: string
tierLevel: number
link: string
org: boolean
sidebarSize?: string
sidebarLogo?: string
[key: string]: unknown
}
const SPONSORS_SOURCE_URL = 'https://raw.githubusercontent.com/middleapi/static/refs/heads/main/sponsors.json'
const ROOT_DIR = process.cwd()
const README_FILE_NAME = 'README.md'
const WEBSITE_SPONSORS_FILE = path.join(ROOT_DIR, 'apps/content/.vitepress/theme/sponsors.ts')
const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', '.output', '.next', '.nuxt', '.turbo'])
async function findReadmes(dir: string): Promise<string[]> {
const entries = await readdir(dir, { withFileTypes: true })
const result: string[] = []
const subdirPromises: Promise<string[]>[] = []
for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
if (SKIP_DIRS.has(entry.name)) {
continue
}
subdirPromises.push(findReadmes(fullPath))
}
else if (entry.isFile() && entry.name === README_FILE_NAME) {
result.push(fullPath)
}
}
const subResults = await Promise.all(subdirPromises)
return result.concat(...subResults)
}
function withTracking(url: string): string {
try {
const tracked = new URL(url)
tracked.searchParams.set('ref', 'orpc')
return tracked.toString()
}
catch {
return url
}
}
function escapeHtml(value: string): string {
return value
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll('\'', ''')
}
function getTierImageSizeAndColumns(tierLevel: number, tierLevels: number[]): [columns: number, imageSize: number] {
const rank = tierLevels.findIndex(level => level === tierLevel)
const columnByRank = [3, 4, 5, 6, 7, 8]
const column = columnByRank[Math.min(rank, columnByRank.length - 1)] ?? 3
return [column, Math.floor(838 / column)]
}
function buildSponsorsSection(sponsors: Sponsor[]): string {
const activeSponsors = sponsors.filter(sponsor => sponsor.tierLevel > 0 && sponsor.amount > 0)
const pastSponsors = sponsors.filter(sponsor => sponsor.tierLevel <= 0 || sponsor.amount <= 0)
const groupedSponsors = new Map<number, Sponsor[]>()
for (const sponsor of activeSponsors) {
const group = groupedSponsors.get(sponsor.tierLevel)
if (group) {
group.push(sponsor)
continue
}
groupedSponsors.set(sponsor.tierLevel, [sponsor])
}
const lines = [
'## Sponsors',
'',
'If you find oRPC valuable and would like to support its development, you can do so here: [GitHub Sponsors](https://github.com/sponsors/dinwwwh).',
'',
]
const tierLevels = [...groupedSponsors.keys()].sort((a, b) => b - a)
for (const tierLevel of tierLevels) {
const tierSponsors = groupedSponsors.get(tierLevel)
if (!tierSponsors || tierSponsors.length === 0) {
continue
}
const tierTitle = tierSponsors[0]?.tierTitle ?? `Tier ${tierLevel}`
const [columns, imageSize] = getTierImageSizeAndColumns(tierLevel, tierLevels)
lines.push(`### ${tierTitle}`)
lines.push('')
lines.push('<table>')
lines.push(' <tr>')
for (const [index, sponsor] of tierSponsors.entries()) {
const href = sponsor.link
const displayName = sponsor.name ?? sponsor.login
const escapedName = escapeHtml(displayName)
lines.push(` <td align="center"><a href="${escapeHtml(href)}" target="_blank" rel="noopener" title="${escapedName}"><img src="${escapeHtml(sponsor.avatar)}" width="${imageSize}" alt="${escapedName}"/><br />${escapedName}</a></td>`)
const isRowEnd = (index + 1) % columns === 0
const isLast = index === tierSponsors.length - 1
if (isRowEnd && !isLast) {
lines.push(' </tr>')
lines.push(' <tr>')
}
}
lines.push(' </tr>')
lines.push('</table>')
lines.push('')
}
if (pastSponsors.length > 0) {
lines.push('### Past Sponsors')
lines.push('')
lines.push('<p>')
for (const sponsor of pastSponsors) {
const href = sponsor.link
const displayName = sponsor.name ?? sponsor.login
const escapedName = escapeHtml(displayName)
lines.push(` <a href="${escapeHtml(href)}" target="_blank" rel="noopener" title="${escapedName}"><img src="${escapeHtml(sponsor.avatar)}" width="32" height="32" alt="${escapedName}" /></a>`)
}
lines.push('</p>')
lines.push('')
}
return `${lines.join('\n')}\n`
}
function replaceSponsorsSection(content: string, replacement: string): string {
const heading = '## Sponsors'
const startIndex = content.indexOf(heading)
if (startIndex === -1) {
return content
}
const nextHeadingIndex = content.indexOf('\n## ', startIndex + heading.length)
const endIndex = nextHeadingIndex === -1 ? content.length : nextHeadingIndex + 1
return `${content.slice(0, startIndex)}${replacement}${content.slice(endIndex)}`
}
function buildWebsiteSponsorsFileContent(sponsors: Sponsor[]): string {
const normalizedSponsors = sponsors.map((sponsor) => {
const createdAt = typeof sponsor.createdAt === 'string' && sponsor.createdAt.length > 0
? sponsor.createdAt
: undefined
const sidebarSize = sponsor.sidebarSize === 'normal' || sponsor.sidebarSize === 'small' || sponsor.sidebarSize === 'none'
? sponsor.sidebarSize
: 'none'
const sidebarLogo = typeof sponsor.sidebarLogo === 'string' && sponsor.sidebarLogo.length > 0
? sponsor.sidebarLogo
: sponsor.avatar
return {
name: sponsor.name,
login: sponsor.login,
avatar: sponsor.avatar,
amount: sponsor.amount,
link: sponsor.link,
org: sponsor.org,
...(createdAt ? { createdAt } : {}),
tierTitle: sponsor.tierTitle,
tierLevel: sponsor.tierLevel,
sidebarSize,
sidebarLogo,
}
})
// eslint-disable-next-line ban/ban
const sponsorsJson = JSON.stringify(normalizedSponsors, null, 2)
return `// This file is auto-generated by scripts/sync-sponsor.ts. Do not edit manually.
export type SidebarPlacementSize = 'normal' | 'small' | 'none'
export interface JSONSponsor {
name: string | null
login: string
avatar: string
amount: number
link: string
org: boolean
createdAt?: string
tierTitle: string
tierLevel: number
sidebarSize: SidebarPlacementSize
sidebarLogo: string
}
export const sponsors: JSONSponsor[] = ${sponsorsJson}
`
}
async function writeWebsiteSponsorsFile(sponsors: Sponsor[]): Promise<void> {
await mkdir(path.dirname(WEBSITE_SPONSORS_FILE), { recursive: true })
const content = buildWebsiteSponsorsFileContent(sponsors)
await writeFile(WEBSITE_SPONSORS_FILE, content)
}
async function main(): Promise<void> {
const response = await fetch(SPONSORS_SOURCE_URL)
if (!response.ok) {
throw new Error(`Failed to fetch sponsors data: ${response.status} ${response.statusText}`)
}
const sponsors = (await response.json() as Sponsor[]).map(sponsor => ({ ...sponsor, link: withTracking(sponsor.link) }))
await writeWebsiteSponsorsFile(sponsors)
const readmeFiles = await findReadmes(ROOT_DIR)
const replacement = buildSponsorsSection(sponsors)
const readmeContents = await Promise.all(
readmeFiles.map(readmePath => readFile(readmePath, 'utf8')),
)
const writePromises: Promise<void>[] = []
let updatedCount = 0
for (const [i, content] of readmeContents.entries()) {
const nextContent = replaceSponsorsSection(content, replacement)
if (nextContent !== content) {
writePromises.push(writeFile(readmeFiles[i]!, nextContent))
updatedCount += 1
}
}
await Promise.all(writePromises)
console.log(`Updated sponsors section in ${updatedCount} README files.`)
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})