Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Bao Zhiyuan committed Nov 21, 2024
1 parent 81154a6 commit 70bc9cb
Showing 1 changed file with 94 additions and 75 deletions.
169 changes: 94 additions & 75 deletions src/build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const md = markdownit({
typographer: true,
})

const FINAL_TEAMS = [
'布丁大馍',
'汪汪立功',
'GoToTheDoor',
'天地一!屋!大爱盟',
'Arc_En_Ciel',
]

function githubBtn(
authorName: string,
repoName: string,
Expand Down Expand Up @@ -45,7 +53,7 @@ type MetaInfo = {
control?: string
readme?: string
cover: boolean
prInfo: Awaited<ReturnType<typeof getPRInfo>>
prInfo?: Awaited<ReturnType<typeof getPRInfo>>
}

const metaInfos = new Map<string, MetaInfo>()
Expand Down Expand Up @@ -78,6 +86,48 @@ async function allPulls() {
return pulls
}

async function collectTeamInfo(teamName: string, pull_number?: number) {
if (!fs.existsSync(`teams/${teamName}/game.wasm`)) {
return
}
const prInfo = pull_number ? await getPRInfo(pull_number) : undefined
const metaInfo: MetaInfo = {
prInfo,
cover: false,
}

const files = fs
.readdirSync(`teams/${teamName}`, { withFileTypes: true })
.filter(d => d.isFile())
.map(d => d.name)

for (const file of files) {
const read = (file: string): string =>
fs.readFileSync(`teams/${teamName}/${file}`, 'utf8')
switch (file) {
case 'cover.png': {
metaInfo.cover = true
continue
}
case 'README.md': {
metaInfo.readme = read(file)
continue
}
case 'title': {
metaInfo.title = read(file)
continue
}
case 'control': {
metaInfo.control = read(file)
continue
}
}
}

console.log(`metainfo of ${teamName}:`, metaInfo)
metaInfos.set(teamName, metaInfo)
}

async function collectMetaInfos(): Promise<void> {
const pulls = await allPulls()
for (const pull of pulls) {
Expand All @@ -102,45 +152,16 @@ async function collectMetaInfos(): Promise<void> {
if (!fs.existsSync(`teams/${teamName}`)) {
continue
}
const prInfo = await getPRInfo(pull_number)
const metaInfo: MetaInfo = {
prInfo,
cover: false,
}

const files = fs
.readdirSync(`teams/${teamName}`, { withFileTypes: true })
.filter(d => d.isFile())
.map(d => d.name)

for (const file of files) {
const read = (file: string): string =>
fs.readFileSync(`teams/${teamName}/${file}`, 'utf8')
switch (file) {
case 'cover.png': {
metaInfo.cover = true
continue
}
case 'README.md': {
metaInfo.readme = read(file)
continue
}
case 'title': {
metaInfo.title = read(file)
continue
}
case 'control': {
metaInfo.control = read(file)
continue
}
}
}

console.log(`metainfo of ${teamName}:`, metaInfo)
metaInfos.set(teamName, metaInfo)
await collectTeamInfo(teamName, pull_number)
}
}
}
const teams = fs.readdirSync('teams', { withFileTypes: true })
for (const team of teams) {
if (!team.isDirectory()) continue
if (metaInfos.has(team.name)) continue
await collectTeamInfo(team.name)
}
}

function renderGameCard(teamName: string, metaInfo: MetaInfo): string {
Expand All @@ -149,22 +170,24 @@ function renderGameCard(teamName: string, metaInfo: MetaInfo): string {
: 'default-cover.png'

const teamPath = querystring.escape(teamName)
const authorName = metaInfo.prInfo.head.user.login
const repoName = metaInfo.prInfo.head.repo?.name

if (repoName === undefined) {
throw new Error(`renderGameCard: repoName is undefined`)
}
const authorName = metaInfo.prInfo?.head.user.login
const repoName = metaInfo.prInfo?.head.repo?.name

const footer = metaInfo.title
? `<h2>${metaInfo.title}</h2><p>${teamName}</p>`
: `<p>${teamName}</p>`

let button
if (authorName && repoName) {
button = githubBtn(authorName, repoName, { large: true })
} else {
button = ''
}
return /*html*/ `
<div class='game-card'>
<a href='${teamPath}/index.html'>
<div class='game-card__star'>
${githubBtn(authorName, repoName, { large: true })}
${button}
</div>
<div class='game-card__cover'>
<img src='${coverPath}'/>
Expand Down Expand Up @@ -346,35 +369,24 @@ function gameIndexHtml(teamName: string, metaInfo: MetaInfo): string {
const title = metaInfo.title ?? teamName
const control = metaInfo.control ?? ''
const readme = metaInfo.readme ? md.render(metaInfo.readme) : ''
const authorName = metaInfo.prInfo.head.user.login
const repoName = metaInfo.prInfo.head.repo?.name
const authorUrl = metaInfo.prInfo.head.user.html_url
const avatarUrl = metaInfo.prInfo.head.user.avatar_url
const updateTime = metaInfo.prInfo.merged_at

if (repoName === undefined || updateTime === null) {
throw new Error(
JSON.stringify(
{
title,
control,
readme,
avatarUrl,
updateTime,
},
null,
2,
),
)
}

const updateDate = new Date(updateTime).toLocaleDateString('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric',
})

const avatar = /*html*/ `
const authorName = metaInfo.prInfo?.head.user.login
const repoName = metaInfo.prInfo?.head.repo?.name
const authorUrl = metaInfo.prInfo?.head.user.html_url
const avatarUrl = metaInfo.prInfo?.head.user.avatar_url
const updateTime = metaInfo.prInfo?.merged_at

const updateDate = updateTime
? new Date(updateTime).toLocaleDateString('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric',
})
: undefined

const avatar =
authorUrl && avatarUrl && authorName && updateDate
? /*html*/ `
<div class="avatar">
<a href="${authorUrl}" class="avatar__photo" target="_blank">
<img src="${avatarUrl}"/>
Expand All @@ -387,6 +399,7 @@ function gameIndexHtml(teamName: string, metaInfo: MetaInfo): string {
</div>
</div>
`
: ''
return /*html*/ `
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -503,9 +516,15 @@ function gameIndexHtml(teamName: string, metaInfo: MetaInfo): string {
<iframe class="wasm4-game" src="game.html" frameborder="0"></iframe>
<p class="control">${control}</p>
<h1>${title}</h1>
<p class="vote">Star 仓库,为 ta 投票
${githubBtn(authorName, repoName, { large: true })}
</p>
${
authorName && repoName
? `<p class="vote">Star 仓库,为 ta 投票 ${githubBtn(
authorName,
repoName,
{ large: false },
)}</p>`
: ''
}
${avatar}
<article>
${readme}
Expand Down

0 comments on commit 70bc9cb

Please sign in to comment.