Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@handy-common-utils/fs-utils
Advanced tools
File system operations related utilities based on fs-extra
File system operations related utilities based on fs-extra
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>');
@handy-common-utils/fs-utils
@handy-common-utils/fs-utils / fs-utils / FsUtils
fs-utils.FsUtils
• new FsUtils()
▸ Static
addSurroundingInFile(filePath
, matchPattern
, addBefore
, addAfter
, fileEncoding?
): Promise
<void
>
Add surrounding content to the matching sections in the text file.
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 |
Promise
<void
>
Promise of void
▸ 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.
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 |
Promise
<void
>
Promise of void
▸ Static
escapeRegExpReplacement(input
): string
Escape the ' sign in the string for using the string as the second argument to String.replace(...)
Name | Type | Description |
---|---|---|
input | string | the original string |
string
a new string with all ' in the original string being replaced by '$'
▸ Static
replaceInFile(filePath
, matchPattern
, replacementOrBuilder
, fileEncoding?
): Promise
<void
>
Replace the matching sections in the text file.
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 replaced. You may want to use these tricks: m flag, g flag, s flag, [\s\S]* , .*? |
replacementOrBuilder | string | (matchPattern : RegExp , filePath : string ) => string | PromiseLike <string > | undefined | The replacement string or a function for building the replacement string. Please note that you can use special replacement patterns but also you need to take care of the escaping. For details of special replacement patterns see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace |
fileEncoding | undefined | string | 'utf-8' | encoding of the file |
Promise
<void
>
Promise of void
▸ Static
replaceInFileWithFileContent(filePath
, matchPattern
, contentFilePath
, fileEncoding?
): Promise
<void
>
Replace the matching sections in the text file with content from another file.
Name | Type | Default value | Description |
---|---|---|---|
filePath | string | undefined | path of the file |
matchPattern | RegExp | undefined | RegExp for deciding which section of the file would be replaced. You must have a capturing group in the pattern. You may want to use these tricks: m flag, g flag, s flag, [\s\S]* , .*? |
contentFilePath | string | undefined | path of the file for getting the replacement content |
fileEncoding | undefined | string | 'utf-8' | encoding of the files |
Promise
<void
>
Promise of void
@handy-common-utils/fs-utils / fs-utils
▸ Const
addSurroundingInFile(filePath
, matchPattern
, addBefore
, addAfter
, fileEncoding?
): Promise
<void
>
Name | Type | Default value |
---|---|---|
filePath | string | undefined |
matchPattern | RegExp | undefined |
addBefore | string | undefined |
addAfter | string | undefined |
fileEncoding | undefined | string | 'utf-8' |
Promise
<void
>
▸ Const
changeFileContent(filePath
, transformContent
, fileEncoding?
): Promise
<void
>
Name | Type | Default value |
---|---|---|
filePath | string | undefined |
transformContent | (originalContent : string , filePath : string ) => string | PromiseLike <string > | undefined |
fileEncoding | undefined | string | 'utf-8' |
Promise
<void
>
▸ Const
escapeRegExpReplacement(input
): string
Name | Type |
---|---|
input | string |
string
▸ Const
replaceInFile(filePath
, matchPattern
, replacementOrBuilder
, fileEncoding?
): Promise
<void
>
Name | Type | Default value |
---|---|---|
filePath | string | undefined |
matchPattern | RegExp | undefined |
replacementOrBuilder | string | (matchPattern : RegExp , filePath : string ) => string | PromiseLike <string > | undefined |
fileEncoding | undefined | string | 'utf-8' |
Promise
<void
>
▸ Const
replaceInFileWithFileContent(filePath
, matchPattern
, contentFilePath
, fileEncoding?
): Promise
<void
>
Name | Type | Default value |
---|---|---|
filePath | string | undefined |
matchPattern | RegExp | undefined |
contentFilePath | string | undefined |
fileEncoding | undefined | string | 'utf-8' |
Promise
<void
>
FAQs
File system operations related utilities based on fs-extra
The npm package @handy-common-utils/fs-utils receives a total of 124 weekly downloads. As such, @handy-common-utils/fs-utils popularity was classified as not popular.
We found that @handy-common-utils/fs-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.