API Overview
1. Directory Operations
FSHelper.createFolder(folderPath)
Creates a folder (including parent directories if needed).
1await FSHelper.createFolder('./myFolder');
Returns:
1{ success: boolean, message: string, path: string }
FSHelper.deleteFolder(folderPath)
Deletes a folder recursively.
1await FSHelper.deleteFolder('./myFolder');
Returns:
1{ success: boolean, message: string, path: string }
FSHelper.moveFolder(oldPath, newPath)
Moves or renames a folder.
1await FSHelper.moveFolder('./oldFolder', './newFolder');
Returns:
1{ success: boolean, message: string, from: string, to: string }
FSHelper.deleteEmptyDirs(dir, options)
Recursively deletes empty directories.
Options:
verbose(boolean) – log actionsdryRun(boolean) – simulate deletionsmaxDepth(number) – maximum recursion depthexclude(string[]) – directory names to skipexcludeRegex(RegExp[]) – regex patterns to skip
1await FSHelper.deleteEmptyDirs('.', { dryRun: true, verbose: true });
2. Path & Existence Checks
FSHelper.pathExists(path)– returnstrueif a path exists.FSHelper.fileExists(filePath)– structured response if file exists.FSHelper.folderExists(folderPath)– structured response if folder exists.FSHelper.isExist– alias forfolderExists.
3. JSON Utilities
FSHelper.writeJSON(filePath, data)
Writes a JavaScript object to a JSON file.
1await FSHelper.writeJSON('./data.json', { hello: 'world' });
FSHelper.readJSON(filePath)
Reads a JSON file into a JavaScript object.
1const result = await FSHelper.readJSON('./data.json');2console.log(result.data);
Returns:
1{ success: boolean, message: string, data?: any, path: string }
4. Directory Listing & Walkers
FSHelper.List.flat(dir, baseDir)
Recursively lists files and directories in a flat structure.
1const files = await FSHelper.List.flat('.', '.');
Output:
1[{ type: 'file'|'directory', name: 'relative/path', size?: number }]
FSHelper.List.tree(dir)
Recursively lists files and directories as a tree structure.
1const tree = await FSHelper.List.tree('.');
Output:
1{2 folder1: { file1: 'file', subfolder: { file2: 'file' } },3 file3: 'file'4}
FSHelper.List.directories(dir)
Lists immediate directory contents (non-recursive).
1const dirs = await FSHelper.List.directories('.');
Output:
1[{ name: 'folder1', type: 'directory' }, { name: 'file.txt', type: 'file' }]
5. Exclusion Utilities
FSHelper.exclude.add(matcher)– add a string, regex, or function to globally exclude files/folders.FSHelper.exclude.should(name)– checks if a file/folder should be excluded.
1FSHelper.exclude.add(/^_/); // exclude all names starting with "_"
6. Utility Functions
FSHelper.getDirectories(dirPath, options)– returns a list of directories, with optional filtering and sorting.- Flexible filtering using regex, strings, or custom functions.
7. Text Replacement Utility
Recursively replaces multiple patterns in all files within a directory.
Parameters:
| Parameter | Type | Description |
|---|---|---|
dir |
string |
Root directory to start replacements |
replacements |
Array<{ value: string, replaced: string }> |
Array of replacement objects {value, replaced} |
Example Usage:
1import { FSHelper } from 'hbh-fs';3(async () => {4 // Replace text in all files recursively5 await FSHelper.replaceInFiles('./myFolder', [6 { value: 'World', replaced: 'Universe' },7 { value: 'hello', replaced: 'hi' }8 ]);10 console.log('✅ Text replacement complete!');11})();
Behavior:
- Traverses all subdirectories recursively.
- Reads each file as UTF-8 text.
- Replaces all occurrences of
valuewithreplaced. - Writes the updated content back to the file.