Skip to content

useFile Hook

The useFile composable accesses the current File context including path, baseName, sources, imports, exports, and metadata.

Use useFile when: You need to read or modify the current file's properties from within a component.

Perfect for: Adding sources dynamically, reading file metadata, conditional file generation.

Usage

Access the current file's properties from within a component.

Example: Read file path and baseName.

ts
import { 
useFile
} from '@kubb/fabric-core'
const
file
=
useFile
()
if (
file
) {
console
.
log
(
file
.
path
)
console
.
log
(
file
.
baseName
)
}

Return Value

Returns the current File object containing:

baseNamestringFile name with extension
pathstringFull path to file
sourcesSource[]Array of source code blocks
importsImport[]Array of import statements
exportsExport[]Array of export statements
metaobjectOptional metadata
bannerstringOptional banner text
footerstringOptional footer text

When to Use

Use useFile when you need to:

  • Access the current file's properties
  • Add sources, imports, or exports programmatically
  • Read file metadata from within a component

Examples

Access File Properties

tsx
import { 
useFile
} from '@kubb/fabric-core'
function
FileInfo
() {
const
file
=
useFile
()
if (!
file
) return null
return `// File: ${
file
.
baseName
} at ${
file
.
path
}`
}

Add Source Dynamically

tsx
import { 
useFile
} from '@kubb/fabric-core'
function
DynamicSource
({
types
}: {
types
: string[] }) {
const
file
=
useFile
()
if (!
file
) return null
types
.
forEach
(
type
=> {
file
.
sources
.
push
({
value
: `export type ${
type
} = {}`,
isExportable
: true,
}) }) return null }

See Also

Released under the MIT License.