Overview
ThegetFileTree function recursively scans a directory and returns a formatted array of file and directory paths. It automatically excludes common build and dependency directories.
Function Signature
Parameters
The directory path to scan. Can be relative or absolute. Relative paths are resolved from the current working directory.
Internal parameter used for indentation during recursive calls. Generally not provided by users. Defaults to an empty string.
Return Value
An array of strings representing the file tree structure. Directories are suffixed with
/ and subdirectories/files are indented with two spaces per level.Implementation Details
The function performs the following steps:- Reads the directory contents using
fs.readdirSync() - Filters out directories listed in the
IGNORED_DIRSarray - For each item:
- If it’s a directory: adds it with a
/suffix and recursively scans it with increased indentation - If it’s a file: adds it to the results with current indentation
- If it’s a directory: adds it with a
- Returns the complete tree as an array of formatted strings
Ignored Directories
The following directories are automatically excluded from the file tree:These directories are commonly used for version control, dependencies, and build artifacts. Excluding them significantly improves performance and readability when scanning project directories.
Usage Example
Output Format
The function returns an array where:- Each directory is suffixed with
/ - Each level of nesting adds two spaces of indentation
- Files and directories are listed in the order they appear in the file system
Error Handling
This is a synchronous recursive operation and will block the event loop until the entire directory tree is scanned. For very large directory structures, this may take noticeable time.