Skip to content

Commit 8188798

Browse files
authored
content: add examples to packages (#238)
1 parent 1569e7f commit 8188798

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+313
-18
lines changed

bin/commands/github/sync-packages.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { defineCommand } from 'citty'
55
import consola from 'consola'
66
import { ofetch } from 'ofetch'
77
import { fetchRepos } from '../../utils/github'
8-
import { addPackage, getPackages, getPackagesPath, removePackage } from '../../utils/content'
8+
import { addPackage, getExamplesLink, getPackages, getPackagesPath, removePackage } from '../../utils/content'
99
import { getPackagesRedirectsPath } from '../../utils/config'
1010
import type { GitHubRepo } from '../../types'
1111

@@ -70,7 +70,12 @@ async function createPR(package_: string, repos: GitHubRepo[], operation: 'add'
7070
consola.fatal(`Repo ${package_} not found.`)
7171
exit(1)
7272
}
73-
addPackage(repo)
73+
const examplesLink = await getExamplesLink(repo.name)
74+
addPackage({
75+
name: repo.name,
76+
description: repo.description,
77+
examples: examplesLink,
78+
})
7479
}
7580
else if (operation === 'remove') {
7681
removePackage(package_)

bin/commands/sync.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const sync = defineCommand({
66
},
77
subCommands: {
88
'packages-redirects': () => import('./sync/packages-redirects').then(m => m.packagesRedirects),
9+
'packages-examples': () => import('./sync/packages-examples').then(m => m.packagesExamples),
910
'packages': () => import('./sync/packages').then(m => m.packages),
1011
},
1112
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { defineCommand } from 'citty'
2+
import { getExamplesLink, getPackages, loadPackageContent, writePackageContent } from '../../utils/content'
3+
4+
export const packagesExamples = defineCommand({
5+
meta: {
6+
name: 'packages-examples',
7+
description: 'Add examples link to packages that does not have it',
8+
},
9+
async run() {
10+
const packages = getPackages()
11+
const packagesContents = []
12+
13+
for (const package_ of packages) {
14+
const content = loadPackageContent(package_)
15+
16+
packagesContents.push(content)
17+
}
18+
19+
// We assume that examples will be in the root of the repo in the examples directory
20+
const examples = await Promise.all(packagesContents.map(async (content) => {
21+
const examplesLink = await getExamplesLink(content.title)
22+
23+
if (!examplesLink) {
24+
// Remove examples link
25+
content.examples = {
26+
link: null,
27+
page: false,
28+
}
29+
30+
return content
31+
}
32+
33+
// Add examples link
34+
content.examples = {
35+
link: examplesLink,
36+
page: content.examples?.page || false,
37+
}
38+
return content
39+
}))
40+
41+
// Write back
42+
for (const example of examples)
43+
writePackageContent(example)
44+
},
45+
})

bin/commands/sync/packages.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineCommand } from 'citty'
22
import { consola } from 'consola'
33
import { fetchRepos } from '../../utils/github'
4-
import { addPackage, getPackages, removePackage } from '../../utils/content'
4+
import { addPackage, getExamplesLink, getPackages, removePackage } from '../../utils/content'
55

66
export const packages = defineCommand({
77
meta: {
@@ -14,9 +14,14 @@ export const packages = defineCommand({
1414
// Repositories without a package
1515
const reposWithoutPackage = repos.filter(repo => !packages.includes(repo.name))
1616

17-
for (const repo of reposWithoutPackage) {
17+
for await (const repo of reposWithoutPackage) {
1818
consola.info(`Creating ${repo.name}`)
19-
addPackage(repo)
19+
const examplesLinks = await getExamplesLink(repo.name)
20+
addPackage({
21+
name: repo.name,
22+
description: repo.description,
23+
examples: examplesLinks,
24+
})
2025
}
2126

2227
// Packages without a repository

bin/types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,29 @@ export interface GithubRelease {
2525
markdown: string
2626
html: string
2727
}
28+
29+
// https://github.com/unjs/ungh#reposownernamefilesbranch
30+
export interface GitHubFile {
31+
path: string
32+
mode: string
33+
sha: string
34+
size: number
35+
}
36+
37+
export interface ContentPackage {
38+
title: string
39+
description: string
40+
github: {
41+
owner: string
42+
repo: string
43+
}
44+
npm?: {
45+
name: string
46+
}
47+
documentation: string
48+
examples?: {
49+
link: string | null
50+
page: boolean
51+
}
52+
playgrounds?: Record<string, string>
53+
}

bin/utils/content.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
22
import { join } from 'node:path'
33
import { cwd } from 'node:process'
4-
import type { GitHubRepo } from '../types'
4+
import yaml from 'js-yaml'
5+
import { ofetch } from 'ofetch'
6+
import type { ContentPackage, GitHubFile } from '../types'
57

68
export function getContentPath() {
79
const currentPath = cwd()
@@ -21,13 +23,21 @@ export function getBlogPath() {
2123
return join(contentPath, '5.blog')
2224
}
2325

26+
export function loadPackageContent(name: string) {
27+
return yaml.load(readFileSync(join(getPackagesPath(), `${name}.yml`), 'utf-8')) as ContentPackage
28+
}
29+
30+
export function writePackageContent(package_: ContentPackage) {
31+
writeFileSync(join(getPackagesPath(), `${package_.title}.yml`), yaml.dump(package_))
32+
}
33+
2434
export function getPackages() {
2535
const packagesPath = getPackagesPath()
2636

2737
return readdirSync(packagesPath).filter(p => p.endsWith('.yml') && !p.startsWith('.')).map(p => p.replace('.yml', ''))
2838
}
2939

30-
export function addPackage(repo: GitHubRepo) {
40+
export function addPackage(repo: { name: string, description: string, examples: string | null }) {
3141
const packagesPath = getPackagesPath()
3242

3343
const template = readFileSync(join(packagesPath, '.template.yml'), 'utf-8')
@@ -37,6 +47,7 @@ export function addPackage(repo: GitHubRepo) {
3747
.replace('package_description', repo.description)
3848
.replace('repo_name', repo.name)
3949
.replace('npm_name', repo.name)
50+
.replace('examples_link', repo.examples || 'null')
4051
.replace('docs_link', `https://github.com/unjs/${repo.name}`))
4152
}
4253

@@ -45,3 +56,11 @@ export function removePackage(name: string) {
4556

4657
rmSync(join(packagesPath, `${name}.yml`))
4758
}
59+
60+
export async function getExamplesLink(name: string) {
61+
const files = await ofetch<{ files: GitHubFile[] }>(`https://ungh.cc/repos/unjs/${name}/files/main`)
62+
63+
const hasExamples = files.files.some(f => f.path.startsWith('examples/'))
64+
65+
return hasExamples ? `https://github.com/unjs/${name}/blob/main/examples` : null
66+
}

content/4.packages/.template.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ npm:
77
name: npm_name
88
playgrounds:
99
examples:
10+
link: examples_link
11+
page: false # Used to create a page using the examples
1012
documentation: docs_link

content/4.packages/bundle-runner.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ github:
66
npm:
77
name: bundle-runner
88
documentation: https://github.com/unjs/bundle-runner
9+
examples:
10+
link: null
11+
page: false

content/4.packages/c12.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ github:
66
npm:
77
name: c12
88
documentation: https://github.com/unjs/c12#%EF%B8%8F-c12
9+
examples:
10+
link: null
11+
page: false

content/4.packages/changelogen.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ github:
66
npm:
77
name: changelogen
88
documentation: https://github.com/unjs/changelogen#changelogen
9+
examples:
10+
link: null
11+
page: false

0 commit comments

Comments
 (0)