Skip to content

Commit

Permalink
remove file system throw methods (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious authored Jan 4, 2025
1 parent 519b591 commit eda5977
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 334 deletions.
22 changes: 22 additions & 0 deletions .changeset/fair-candles-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'renoun': major
---

Removes all `*OrThrow` methods from `Directory` and `EntryGroup`. This also exports two new custom errors, `FileNotFoundError` and `FileExportNotFoundError` to handle missing files and exports.

### Breaking Changes

`Directory` and `EntryGroup` no longer have `*OrThrow` methods, use the respective methods instead. To get the same functionality as before, you can catch the error and handle it accordingly:

```ts
import { Directory } from 'renoun/file-system'

const posts = new Directory({ path: 'posts' })

posts.getFile('hello-world', 'mdx').catch((error) => {
if (error instanceof FileNotFoundError) {
return undefined
}
throw error
})
```
4 changes: 2 additions & 2 deletions .changeset/few-swans-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default async function Page({
params: Promise<{ slug: string }>
}) {
const slug = (await params).slug
const post = await posts.getFileOrThrow(slug, 'mdx')
const Content = await post.getExportValueOrThrow('default')
const post = await posts.getFile(slug, 'mdx')
const Content = await post.getExportValue('default')

return <Content />
}
Expand Down
2 changes: 1 addition & 1 deletion apps/site/app/(home)/QuickSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default async function Page({ params }: { params: Promise<{ slug: string
return <div>Post not found</div>
}
const Content = await post.getExportValueOrThrow('default')
const Content = await post.getExportValue('default')
return <Content />
}`,
Expand Down
45 changes: 34 additions & 11 deletions apps/site/app/(site)/components/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
isFile,
isDirectory,
type JavaScriptFileExport,
FileNotFoundError,
FileExportNotFoundError,
} from 'renoun/file-system'
import { APIReference, CodeBlock, Tokens } from 'renoun/components'
import type { MDXHeadings } from 'renoun/mdx'
Expand All @@ -28,22 +30,43 @@ export default async function Component({
}) {
const slug = (await params).slug
const componentEntry = await ComponentsCollection.getFile(slug, ['ts', 'tsx'])

if (!componentEntry) {
notFound()
}

const mdxFile = await ComponentsCollection.getFile(slug, 'mdx')
const mdxHeadings = await mdxFile?.getExportValue('headings')
const mdxFile = await ComponentsCollection.getFile(slug, 'mdx').catch(
(error) => {
if (error instanceof FileNotFoundError) {
return undefined
}
throw error
}
)
const mdxHeadings = await mdxFile
?.getExportValue('headings')
.catch((error) => {
if (error instanceof FileExportNotFoundError) {
return undefined
}
throw error
})
const Content = await mdxFile?.getExportValue('default')
const componentDirectory = isDirectory(componentEntry)
? componentEntry
: componentEntry.getParent()
const mainExport = await componentEntry.getExport<any>(
componentEntry.getBaseName()
)
const mainExport = await componentEntry
.getExport<any>(componentEntry.getBaseName())
.catch((error) => {
if (error instanceof FileExportNotFoundError) {
return undefined
}
throw error
})
const description = mainExport ? mainExport.getDescription() : null
const examplesEntry = await componentDirectory.getEntry('examples')
const examplesEntry = await componentDirectory
.getEntry('examples')
.catch((error) => {
if (error instanceof FileNotFoundError) {
return undefined
}
throw error
})
const exampleFiles = examplesEntry
? isDirectory(examplesEntry)
? (
Expand Down
2 changes: 1 addition & 1 deletion apps/site/app/(site)/docs/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function Doc({
params: Promise<{ slug: string[] }>
}) {
const { slug } = await params
const file = await DocsCollection.getFileOrThrow(slug, 'mdx')
const file = await DocsCollection.getFile(slug, 'mdx')

return <DocumentEntry file={file} entryGroup={CollectionGroup} />
}
2 changes: 1 addition & 1 deletion apps/site/app/(site)/guides/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function Guide({
params: Promise<{ slug: string[] }>
}) {
const { slug } = await params
const file = await GuidesCollection.getFileOrThrow(slug, 'mdx')
const file = await GuidesCollection.getFile(slug, 'mdx')

return <DocumentEntry file={file} entryGroup={CollectionGroup} />
}
2 changes: 1 addition & 1 deletion apps/site/app/(site)/guides/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GuidesCollection } from '@/collections'
import { DocumentEntry } from '@/components/DocumentEntry'

export default async function Guides() {
const file = await GuidesCollection.getFileOrThrow('index', 'mdx')
const file = await GuidesCollection.getFile('index', 'mdx')

return (
<DocumentEntry
Expand Down
6 changes: 3 additions & 3 deletions apps/site/app/(site)/utilities/file-system/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FileSystemCollection } from '@/collections'
import { TableOfContents } from '@/components/TableOfContents'

export default async function Page() {
const sourceFile = await FileSystemCollection.getFileOrThrow('index', 'tsx')
const sourceFile = await FileSystemCollection.getFile('index', 'tsx')

if (!sourceFile) {
return null
Expand All @@ -16,8 +16,8 @@ export default async function Page() {
return null
}

const Content = await docFile.getExportValueOrThrow('default')
const headings = await docFile.getExportValueOrThrow('headings')
const Content = await docFile.getExportValue('default')
const headings = await docFile.getExportValue('headings')
const fileExports = await sourceFile.getExports()

return (
Expand Down
6 changes: 3 additions & 3 deletions apps/site/components/DocumentEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export async function DocumentEntry({
shouldRenderTableOfContents?: boolean
shouldRenderUpdatedAt?: boolean
}) {
const Content = await file.getExportValueOrThrow('default')
const metadata = await file.getExportValueOrThrow('metadata')
const headings = await file.getExportValueOrThrow('headings')
const Content = await file.getExportValue('default')
const metadata = await file.getExportValue('metadata')
const headings = await file.getExportValue('headings')
const updatedAt = shouldRenderUpdatedAt
? await file.getLastCommitDate()
: null
Expand Down
25 changes: 22 additions & 3 deletions apps/site/components/SiblingLink.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Link from 'next/link'
import {
FileExportNotFoundError,
FileNotFoundError,
isDirectory,
isJavaScriptFile,
type FileSystemEntry,
Expand Down Expand Up @@ -125,12 +127,24 @@ async function resolveEntryMetadata(entry: FileSystemEntry) {
let file: JavaScriptFile<Metadata>

if (isDirectory(entry)) {
const indexFile = await entry.getFile('index', ['ts', 'tsx'])
const indexFile = await entry
.getFile('index', ['ts', 'tsx'])
.catch((error) => {
if (error instanceof FileNotFoundError) {
return undefined
}
throw error
})

if (indexFile) {
file = indexFile
} else {
const readmeFile = await entry.getFile('readme', 'mdx')
const readmeFile = await entry.getFile('readme', 'mdx').catch((error) => {
if (error instanceof FileNotFoundError) {
return undefined
}
throw error
})

if (readmeFile) {
file = readmeFile as unknown as JavaScriptFile<Metadata>
Expand All @@ -144,7 +158,12 @@ async function resolveEntryMetadata(entry: FileSystemEntry) {
return
}

const metadataExport = await file.getExport('metadata')
const metadataExport = await file.getExport('metadata').catch((error) => {
if (error instanceof FileExportNotFoundError) {
return undefined
}
throw error
})

if (metadataExport) {
return metadataExport.getRuntimeValue()
Expand Down
8 changes: 7 additions & 1 deletion apps/site/components/Sidebar/TreeNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
FileExportNotFoundError,
isFile,
isJavaScriptFile,
type Directory,
Expand All @@ -19,7 +20,12 @@ async function ListNavigation({
const depth = entry.getDepth()
const metadata =
variant === 'title' && isJavaScriptFile(entry)
? await entry.getExportValueOrThrow('metadata')
? await entry.getExportValue('metadata').catch((error) => {
if (error instanceof FileExportNotFoundError) {
return undefined
}
throw error
})
: null

if (isFile(entry)) {
Expand Down
7 changes: 1 addition & 6 deletions apps/site/docs/02.getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ export default async function Page({
}) {
const slug = (await params).slug
const post = await posts.getFile(slug, 'mdx')

if (!post) {
return <div>Post not found</div>
}

const Content = await post.getExportValueOrThrow('default')
const Content = await post.getExportValue('default')

return <Content />
}
Expand Down
7 changes: 1 addition & 6 deletions apps/site/guides/02.next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,7 @@ export default async function Page({
}) {
const slug = (await params).slug
const post = await posts.getFile(slug, 'mdx')

if (!post) {
return <div>Post not found</div>
}

const Content = await post.getExportValueOrThrow('default')
const Content = await post.getExportValue('default')

return <Content />
}
Expand Down
2 changes: 1 addition & 1 deletion apps/site/guides/AllGuides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function AllGuides() {
const entries = await GuidesCollection.getEntries()

return entries.map(async (entry, index) => {
const metadata = await entry.getExportValueOrThrow('metadata')
const metadata = await entry.getExportValue('metadata')

return (
<Card
Expand Down
6 changes: 3 additions & 3 deletions examples/blog/app/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export default async function Page({
}: {
params: Promise<{ slug: string }>
}) {
const post = await posts.getFileOrThrow((await params).slug, 'mdx')
const frontmatter = await post.getExportValueOrThrow('frontmatter')
const post = await posts.getFile((await params).slug, 'mdx')
const frontmatter = await post.getExportValue('frontmatter')
const formattedDate = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZone: 'UTC',
}).format(frontmatter.date)
const Content = await post.getExportValueOrThrow('default')
const Content = await post.getExportValue('default')

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async function Page() {
<ul>
{allPosts.map(async (post) => {
const path = post.getPath()
const frontmatter = await post.getExportValueOrThrow('frontmatter')
const frontmatter = await post.getExportValue('frontmatter')

return (
<li key={path}>
Expand Down
4 changes: 2 additions & 2 deletions examples/blog/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const posts = new Directory({
),
},
sort: async (a, b) => {
const aFrontmatter = await a.getExportValueOrThrow('frontmatter')
const bFrontmatter = await b.getExportValueOrThrow('frontmatter')
const aFrontmatter = await a.getExportValue('frontmatter')
const bFrontmatter = await b.getExportValue('frontmatter')

return bFrontmatter.date.getTime() - aFrontmatter.date.getTime()
},
Expand Down
30 changes: 25 additions & 5 deletions examples/design-system/app/components/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { APIReference } from 'renoun/components'
import {
isFile,
isDirectory,
FileNotFoundError,
type FileSystemEntry,
type JavaScriptFileExport,
} from 'renoun/file-system'
Expand Down Expand Up @@ -31,10 +32,29 @@ export default async function Component({
const componentDirectory = isDirectory(componentEntry)
? componentEntry
: componentEntry.getParent()
const mainEntry =
(await componentDirectory.getFile(slug, ['ts', 'tsx'])) ||
(await componentDirectory.getFile('index', ['ts', 'tsx']))
const examplesEntry = await componentDirectory.getEntry('examples')
const mainEntry = await componentDirectory
.getFile(slug, ['ts', 'tsx'])
.catch((error) => {
if (error instanceof FileNotFoundError) {
return componentDirectory
.getFile('index', ['ts', 'tsx'])
.catch((error) => {
if (error instanceof FileNotFoundError) {
return undefined
}
throw error
})
}
throw error
})
const examplesEntry = await componentDirectory
.getEntry('examples')
.catch((error) => {
if (error instanceof FileNotFoundError) {
return undefined
}
throw error
})
const exampleFiles = examplesEntry
? isDirectory(examplesEntry)
? (await examplesEntry.getEntries()).filter((entry) =>
Expand All @@ -44,7 +64,7 @@ export default async function Component({
? [examplesEntry]
: null
: null
const readmeFile = await componentDirectory.getFileOrThrow('readme', 'mdx')
const readmeFile = await componentDirectory.getFile('readme', 'mdx')
const Readme = await readmeFile.getExportValue('default')
const lastCommitDate = await componentEntry.getLastCommitDate()
const parentDirectory = componentEntry.getParent()
Expand Down
4 changes: 2 additions & 2 deletions packages/renoun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ const posts = new Directory({
Now when calling `JavaScript#getExportValue` and `JavaScriptExport#getRuntimeValue` we get stronger type-checking and autocompletion:

```tsx
const file = await posts.getFileOrThrow('hello-world', 'mdx')
const frontmatter = await file.getExportValueOrThrow('frontmatter')
const file = await posts.getFile('hello-world', 'mdx')
const frontmatter = await file.getExportValue('frontmatter')

frontmatter.title // string
frontmatter.date // Date
Expand Down
4 changes: 2 additions & 2 deletions packages/renoun/src/file-system/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default async function Page({
return <div>Post not found</div>
}

const Content = await post.getExportValueOrThrow('default')
const Content = await post.getExportValue('default')

return <Content />
}
Expand Down Expand Up @@ -76,7 +76,7 @@ export default async function Page() {
<ul>
{allPosts.map(async (post) => {
const path = post.getPath()
const frontmatter = await post.getExportValueOrThrow('frontmatter')
const frontmatter = await post.getExportValue('frontmatter')

return (
<li key={path}>
Expand Down
Loading

0 comments on commit eda5977

Please sign in to comment.