README
@handy-common-utils/fs-utils
File system operations related utilities based on fs-extra
How to use
First add it as a dependency:
npm install @handy-common-utils/fs-utils
Then you can use it in the code:
import { FsUtils } from '@handy-common-utils/fs-utils';
const [,, filePath, matchPattern, beforeString, afterString] = process.argv;
await FsUtils.addSurroundingInFile(filePath, new RegExp(matchPattern), beforeString, afterString);
You can either import and use the class as shown above, or you can import individual functions directly like below:
import { addSurroundingInFile } from 'fs-utils';
await addSurroundingInFile(README_MD_FILE, /<example>(.*?)<\/example>/gms, '<example><b>', '</b></example>');
API
@handy-common-utils/fs-utils
@handy-common-utils/fs-utils
Table of contents
Modules
Classes
@handy-common-utils/fs-utils / fs-utils / FsUtils
Class: FsUtils
fs-utils.FsUtils
Table of contents
Constructors
Methods
- addSurroundingInFile
- changeFileContent
- escapeRegExpReplacement
- replaceInFile
- replaceInFileWithFileContent
Constructors
constructor
• new FsUtils()
Methods
addSurroundingInFile
▸ Static
addSurroundingInFile(filePath
, matchPattern
, addBefore
, addAfter
, fileEncoding?
): Promise
<void
>
Add surrounding content to the matching sections in the text file.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
filePath |
string |
undefined |
path to the file |
matchPattern |
RegExp |
undefined |
RegExp for deciding which section of the file would be processed. You must have a capturing group in the pattern. You may want to use these tricks: m flag, g flag, s flag, [\s\S]* , .*? |
addBefore |
string |
undefined |
the string to be added before the capturing group, no need to escape anything |
addAfter |
string |
undefined |
the string to be added before the capturing group, no need to escape anything |
fileEncoding |
undefined | string |
'utf-8' |
encoding of the file |
Returns
Promise
<void
>
Promise of void
changeFileContent
▸ Static
changeFileContent(filePath
, transformContent
, fileEncoding?
): Promise
<void
>
Change the text file content. This function loads the full content of the file into memory as string, so that it is not suitable for huge (for example, > 500MB) files. If the new content and original content are the same, the file won't be touched.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
filePath |
string |
undefined |
path to the file |
transformContent |
(originalContent : string , filePath : string ) => string | PromiseLike <string > |
undefined |
function for getting the new file content |
fileEncoding |
undefined | string |
'utf-8' |
encoding of the file |
Returns
Promise
<void
>
Promise of void
escapeRegExpReplacement
▸ Static
escapeRegExpReplacement(input
): string
Escape the ' sign in the string for using the string as the second argument to String.replace(...)
Parameters
Name | Type | Description |
---|---|---|
input |
string |
the original string |
Returns
string
a new string with all ' in the original string being replaced by '