Skip to content

Commit a9de1a6

Browse files
committed
feat: make username optional
1 parent d94eb5c commit a9de1a6

4 files changed

Lines changed: 30 additions & 12 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"type": "module",
1818
"scripts": {
1919
"ci": "pnpm install --frozen-lockfile",
20+
"dev": "rm -rf build && mkdir build && cp README.md package.json build/ && tsc --watch",
2021
"build": "tsc && cp README.md package.json build/",
2122
"test": "echo \"Error: no test specified\" && exit 0"
2223
},

src/loader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function reloadProjects(
2828
...opts,
2929
lastUpdated,
3030
})
31+
3132
await reloadOverrides(options)
3233
const projects = await getProjectsList(options)
3334

src/parser.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ export async function getProjectsList(
1818
): Promise<GitHubProjectType[]> {
1919
logger.log(`Fetching projects list from GitHub (since: ${formatDate(options.lastUpdated)})`)
2020

21-
const repos = await fetchRepos(
22-
`https://api.github.com/users/${options.username}/repos?per_page=100`,
23-
options,
24-
)
21+
if (!options.username && (!options.orgs || options.orgs.length === 0)) {
22+
logger.error('No username or orgs provided')
23+
return []
24+
}
25+
26+
const repos: GitHubRepositoryAPIResponse[] = []
27+
if (options.username) {
28+
repos.push(
29+
...(await fetchRepos(
30+
`https://api.github.com/users/${options.username}/repos?per_page=100`,
31+
options,
32+
)),
33+
)
34+
}
2535
for (const org of options.orgs ?? []) {
2636
repos.push(
2737
...(await fetchRepos(`https://api.github.com/orgs/${org}/repos?per_page=100`, options)),
@@ -89,7 +99,7 @@ export async function getProjectsList(
8999
)
90100
const readme = readmeResponse.ok ? await readmeResponse.text() : undefined
91101
project.readme = readme
92-
logger.log(`Rendering README for ${repo.name}`)
102+
logger.log(`Rendering README for ${repo.full_name}`)
93103
const { html } = await parseMarkdown(readme ?? '')
94104
project.readmeHtml = html
95105
projects.push(project)

src/types.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { z } from 'zod'
77
export const LinkSchema = z.object({
88
title: z.string(),
99
href: z.string(),
10-
icon: z.string(),
10+
icon: z.string().optional(),
1111
})
1212

1313
/**
@@ -19,7 +19,7 @@ export type TLinkSchema = {
1919
/** The URL the link points to. */
2020
href: string
2121
/** The icon associated with the link. */
22-
icon: string
22+
icon?: string
2323
}
2424

2525
/**
@@ -76,7 +76,7 @@ export type GitHubRepositoryAPIResponse = NonNullable<
7676
* Schema for loader options.
7777
*/
7878
export const LoaderOptions = z.object({
79-
username: z.string(),
79+
username: z.string().optional(),
8080
debug: z.boolean(),
8181
orgs: z.array(z.string()).optional(),
8282
filter: z
@@ -93,12 +93,18 @@ export const LoaderOptions = z.object({
9393
* Schema for loader options.
9494
*/
9595
export type LoaderOptionsType = {
96-
/** The username from GitHub to fetch the repositories from. */
97-
username: string
96+
/**
97+
* The username from GitHub to fetch the repositories from.
98+
* At least one of `username`, `orgs` must be present in order to have any content to load.
99+
*/
100+
username?: string
101+
/**
102+
* GitHub organizations to fetch projects from.
103+
* At least one of `username`, `orgs` must be present in order to have any content to load.
104+
*/
105+
orgs?: string[]
98106
/** Debug flag - if true, the loader will output debug information. */
99107
debug?: boolean
100-
/** GitHub organizations to fetch projects from. */
101-
orgs?: string[]
102108
/** A function to filter out projects. Filtered projects will not be parsed or saved in the collection. */
103109
// eslint-disable-next-line no-unused-vars
104110
filter?: (project: GitHubRepositoryAPIResponse) => boolean

0 commit comments

Comments
 (0)