@hyperfrontend/project-scope/project/traversal

traversal

Directory-walking and file-search helpers built around an explicit visitor pattern.

walkDirectory and walkTree traverse a folder hierarchy, invoking a WalkVisitor for every entry; the visitor's WalkVisitorResult controls whether to descend, skip, or stop. findFiles, findFilesInTree, and findDirectories are the higher-level shortcuts for "give me everything matching this pattern" use cases, with FindOptions covering depth limits, include/exclude globs, and symlink behavior. Designed for static analysis tools that need predictable, side-effect-free directory iteration with backpressure-style control.

API Reference§

ƒ Functions

§function

findDirectories(startPath: string, patterns: string | string[], options?: FindOptions): string[]

Searches a directory tree for directories matching one or more glob patterns, returning relative or absolute paths based on options.
Parameters
NameTypeDescription
§startPath
stringRoot directory to begin the search
§patterns
string | string[]Glob patterns to filter directories (supports wildcards)
§options?
FindOptionsConfiguration for search behavior
Returns
string[]
List of relative directory paths that match the patterns
Example

Finding directories by pattern

import { findDirectories } from '@hyperfrontend/project-scope'

const componentDirs = findDirectories('./src', 'components')
// => ['features/auth/components', 'features/dashboard/components']
§function

findFiles(startPath: string, patterns: string | string[], options?: FindOptions): string[]

Searches a directory tree for files matching one or more glob patterns, returning relative or absolute paths based on options.
Parameters
NameTypeDescription
§startPath
stringRoot directory to begin the search
§patterns
string | string[]Glob patterns (e.g., '.ts', '/.json') to filter files
§options?
FindOptionsConfiguration for search behavior
Returns
string[]
List of relative file paths that match the patterns
Example

Finding files by pattern

import { findFiles } from '@hyperfrontend/project-scope'

// Find all TypeScript files
const tsFiles = findFiles('./src', '\*\*/*.ts')

// Find multiple file types
const configFiles = findFiles('./', ['\*.json', '\*.yaml', '\*.yml'])

// Limit results and get absolute paths
const first10 = findFiles('./src', '\*\*/*.ts', {
  maxResults: 10,
  absolutePaths: true
})
§function

findFilesInTree(tree: Tree, patterns: string | string[], options?: FindOptions): string[]

Searches a virtual file system tree for files matching glob patterns, useful for analyzing project structure without disk I/O.
Parameters
NameTypeDescription
§tree
TreeIn-memory virtual file system representation
§patterns
string | string[]Glob patterns (e.g., '.ts', '/.json') to filter files
§options?
FindOptionsConfiguration for search behavior
Returns
string[]
List of virtual file paths that match the patterns
Example

Finding files in a virtual tree

import { createTree, findFilesInTree } from '@hyperfrontend/project-scope'

const tree = createTree('/workspace')
const tsFiles = findFilesInTree(tree, '**/*.ts', { maxDepth: 3 })
// => ['src/index.ts', 'src/utils/helpers.ts']
§function

walkDirectory(startPath: string, visitor: WalkVisitor, options?: WalkOptions): void

Traverses a directory tree synchronously, calling a visitor function for each file and directory encountered. Supports depth limiting, hidden file filtering, and gitignore pattern matching.
Parameters
NameTypeDescription
§startPath
stringRoot directory to begin traversal
§visitor
WalkVisitorCallback function invoked for each file system entry
§options?
WalkOptionsConfiguration for traversal behavior
Example

Walking a directory tree

import { walkDirectory } from '@hyperfrontend/project-scope'

const tsFiles: string[] = []
walkDirectory('./src', (entry) => {
  if (entry.isFile && entry.name.endsWith('.ts')) {
    tsFiles.push(entry.relativePath)
  }
}, { maxDepth: 5, respectGitignore: true })
§function

walkTree(tree: Tree, startPath: string, visitor: WalkVisitor, options?: WalkOptions): void

Traverses a virtual file system tree, calling a visitor function for each file and directory. Operates on in-memory tree structure without disk I/O.
Parameters
NameTypeDescription
§tree
TreeIn-memory virtual file system representation
§startPath
stringRoot path within the tree to begin traversal
§visitor
WalkVisitorCallback function invoked for each tree entry
§options?
WalkOptionsConfiguration for traversal behavior
Example

Walking a virtual tree

import { createTree, walkTree } from '@hyperfrontend/project-scope'

const tree = createTree('/workspace')
walkTree(tree, 'src', (entry) => {
  if (entry.isDirectory) {
    console.log('Dir:', entry.relativePath)
    return 'skip' // Don't recurse into this directory
  }
})

◈ Interfaces

§interface

FindOptions

Options for file and directory search operations.
Properties
§absolutePaths?:booleanReturn absolute paths
§maxResults?:numberMaximum results to return
§maxDepth?:numberMaximum depth (-1 for unlimited)
§includeHidden?:booleanInclude hidden files (dotfiles)
§ignorePatterns?:string[]Glob patterns to ignore
§respectGitignore?:booleanRespect .gitignore
§interface

WalkEntry

Walk entry representing a file or directory.
Properties
§name:stringEntry basename
§path:stringFull path
§relativePath:stringRelative path from start
§isFile:booleanIs a file
§isDirectory:booleanIs a directory
§depth:numberDepth from start directory
§interface

WalkOptions

Configuration for directory traversal behavior.
Properties
§maxDepth?:numberMaximum depth (-1 for unlimited)
§includeHidden?:booleanInclude hidden files (dotfiles)
§ignorePatterns?:string[]Glob patterns to ignore
§respectGitignore?:booleanRespect .gitignore

◆ Types

§type

WalkVisitor

Walk visitor function.
type WalkVisitor = (entry: WalkEntry) => WalkVisitorResult
§type

WalkVisitorResult

Visitor function signature.
type WalkVisitorResult = void | "skip" | "stop"