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.