11import { readFileSync , readdirSync , rmSync , writeFileSync } from 'node:fs'
22import { join } from 'node:path'
33import { 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
68export 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+
2434export 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+ }
0 commit comments