|
| 1 | +import 'dotenv/config' |
| 2 | +import { writeFileSync } from 'node:fs' |
| 3 | +import { dirname, join } from 'node:path' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { env } from 'node:process' |
| 6 | +import { $fetch } from 'ofetch' |
| 7 | +import { consola } from 'consola' |
| 8 | +import type { Repository } from './repository.data' |
| 9 | +import { repositoryMeta } from './meta' |
| 10 | + |
| 11 | +const GITHUB_TOKEN = env.GITHUB_TOKEN |
| 12 | + |
| 13 | +const gql = `#graphql |
| 14 | +query repositoryQuery($owner: String!, $name: String!, $readme: String!) { |
| 15 | + repository(owner: $owner, name: $name) { |
| 16 | + name |
| 17 | + stargazers { |
| 18 | + totalCount |
| 19 | + } |
| 20 | + owner { |
| 21 | + avatarUrl |
| 22 | + login |
| 23 | + } |
| 24 | + description |
| 25 | + primaryLanguage { |
| 26 | + name |
| 27 | + color |
| 28 | + } |
| 29 | + forkCount |
| 30 | + object(expression: $readme) { |
| 31 | + ... on Blob { |
| 32 | + text |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +}` |
| 37 | + |
| 38 | +async function fetchRepo(meta: { |
| 39 | + owner: string |
| 40 | + name: string |
| 41 | + readme?: string |
| 42 | +}) { |
| 43 | + const { owner, name, readme } = meta |
| 44 | + |
| 45 | + const _readme = readme || 'main:README.md' |
| 46 | + try { |
| 47 | + const results = await $fetch('https://api.github.com/graphql', { |
| 48 | + method: 'POST', |
| 49 | + headers: { |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + 'Authorization': `Bearer ${GITHUB_TOKEN}`, |
| 52 | + }, |
| 53 | + body: JSON.stringify({ |
| 54 | + query: gql, |
| 55 | + variables: { |
| 56 | + owner, |
| 57 | + name, |
| 58 | + readme: _readme, |
| 59 | + }, |
| 60 | + }), |
| 61 | + }) |
| 62 | + |
| 63 | + const repositoryInfo = results.data.repository as Repository |
| 64 | + |
| 65 | + const markdownFrontmatter = `--- |
| 66 | +title: ${repositoryInfo.name} |
| 67 | +owner: ${repositoryInfo.owner.login} |
| 68 | +name: ${repositoryInfo.name} |
| 69 | +stars: ${repositoryInfo.stargazers.totalCount} |
| 70 | +forks: ${repositoryInfo.forkCount} |
| 71 | +outline: deep |
| 72 | +--- |
| 73 | +
|
| 74 | +<RepoInfo :owner="$frontmatter.owner" :name="$frontmatter.name" :stars="$frontmatter.stars" :forks="$frontmatter.forks" /> |
| 75 | +
|
| 76 | +--- |
| 77 | +
|
| 78 | +` |
| 79 | + |
| 80 | + writeFileSync( |
| 81 | + join(dirname(fileURLToPath(import.meta.url)), `../../showcase/${name}.md`), |
| 82 | + markdownFrontmatter + repositoryInfo.object.text, |
| 83 | + ) |
| 84 | + consola.success(`[${name}.md]: generate success`) |
| 85 | + return repositoryInfo |
| 86 | + } |
| 87 | + catch (error) { |
| 88 | + consola.error(`[${name}.md]: generate failed: ${error}`) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function main() { |
| 93 | + if (!GITHUB_TOKEN) { |
| 94 | + consola.error('GITHUB_TOKEN is missing, please refer to https://github.com/unplugin/docs#development') |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + const fetchs = repositoryMeta.map((repository) => { |
| 99 | + return fetchRepo({ |
| 100 | + name: repository.name, |
| 101 | + owner: repository.owner, |
| 102 | + readme: repository.defaultBranch ? `${repository.defaultBranch}:README.md` : 'main:README.md', |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + Promise.allSettled(fetchs).then((res) => { |
| 107 | + const repoMeta = res?.map((item) => { |
| 108 | + if (item.status === 'fulfilled') { |
| 109 | + return { |
| 110 | + name: item.value?.name, |
| 111 | + stargazers: item.value?.stargazers, |
| 112 | + owner: item.value?.owner, |
| 113 | + description: item.value?.description, |
| 114 | + url: item.value?.url, |
| 115 | + isTemplate: item.value?.isTemplate, |
| 116 | + primaryLanguage: item.value?.primaryLanguage, |
| 117 | + forkCount: item.value?.forkCount, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return null |
| 122 | + })?.filter(item => item && item.name) |
| 123 | + |
| 124 | + writeFileSync( |
| 125 | + join(dirname(fileURLToPath(import.meta.url)), './repository.json'), |
| 126 | + JSON.stringify(repoMeta, null, 2), |
| 127 | + ) |
| 128 | + consola.success('[repository.json] generate success!') |
| 129 | + consola.success('All files generate done!') |
| 130 | + }).catch((error) => { |
| 131 | + consola.error(error) |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +main() |
0 commit comments