-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support passing a file path to APIReference
- Loading branch information
1 parent
8822ce6
commit fc1e9a6
Showing
6 changed files
with
190 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
'renoun': minor | ||
--- | ||
|
||
Adds support for passing a file path to the `APIReference.source` prop: | ||
|
||
```tsx | ||
import { APIReference } from 'renoun/components' | ||
|
||
export function FilePath() { | ||
return ( | ||
<APIReference | ||
source="./GitProvider.tsx" | ||
workingDirectory={import.meta.url} | ||
/> | ||
) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Node, Project } from 'ts-morph' | ||
import tsMorph from 'ts-morph' | ||
import { resolve } from 'node:path' | ||
|
||
import { resolveType } from '../project/index.js' | ||
import type { SymbolFilter } from '../utils/resolve-type.js' | ||
|
||
const projectCache = new Map<string, Project>() | ||
|
||
export function getProject( | ||
tsConfigFilePath: string = 'tsconfig.json' | ||
): Project { | ||
if (!projectCache.has(tsConfigFilePath)) { | ||
const project = new tsMorph.Project({ | ||
skipAddingFilesFromTsConfig: true, | ||
tsConfigFilePath, | ||
}) | ||
projectCache.set(tsConfigFilePath, project) | ||
} | ||
return projectCache.get(tsConfigFilePath)! | ||
} | ||
|
||
export async function getExportedTypes( | ||
filePath: string, | ||
filter?: SymbolFilter, | ||
workingDirectory: string = process.cwd(), | ||
tsConfigFilePath: string = 'tsconfig.json' | ||
) { | ||
const project = getProject(tsConfigFilePath) | ||
|
||
try { | ||
const resolvedFilePath = resolve(workingDirectory, filePath) | ||
const sourceFile = project.addSourceFileAtPath(resolvedFilePath) | ||
const exportedDeclarations = Array.from( | ||
sourceFile.getExportedDeclarations() | ||
) | ||
|
||
return Promise.all( | ||
exportedDeclarations.flatMap(([, declarations]) => { | ||
return declarations.flatMap((declaration) => | ||
resolveType({ | ||
declaration, | ||
filter, | ||
projectOptions: { tsConfigFilePath }, | ||
useClient: false, | ||
}) | ||
) | ||
}) | ||
) | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to add source file at path: ${filePath}\nCurrent working directory: ${workingDirectory}`, | ||
{ cause: error } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
import { APIReference } from 'renoun/components' | ||
|
||
export function FilePath() { | ||
return ( | ||
<APIReference | ||
source="./GitProvider.tsx" | ||
workingDirectory={import.meta.url} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters