Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@handy-common-utils/fs-utils

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@handy-common-utils/fs-utils - npm Package Compare versions

Comparing version 1.0.12 to 1.0.14

dist/bin/replace-in-file.d.ts

2

dist/fs-utils.js

@@ -5,3 +5,3 @@ "use strict";

const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs-extra"));
const fs = (0, tslib_1.__importStar)(require("fs-extra"));
class FsUtils {

@@ -8,0 +8,0 @@ /**

{
"name": "@handy-common-utils/fs-utils",
"version": "1.0.12",
"version": "1.0.14",
"description": "File system operations related utilities based on fs-extra",

@@ -19,12 +19,12 @@ "scripts": {

"add-surrounding-in-file": "dist/bin/add-surrounding-in-file.js",
"replace-in-file": "dist/bin/replace-in-file.js",
"replace-in-file-with-file-content": "dist/bin/replace-in-file-with-file-content.js"
},
"dependencies": {
"fs-extra": "^9.0.1"
"fs-extra": "^8.1.0"
},
"devDependencies": {
"@handy-common-utils/dev-dependencies": "^1.0.11",
"@types/fs-extra": "^9.0.2",
"@handy-common-utils/dev-dependencies": "^1.0.26",
"@types/fs-extra": "^8.1.2",
"@types/tmp": "^0.2.0",
"es-check": "^5.1.2",
"tmp": "^0.2.1"

@@ -31,0 +31,0 @@ },

@@ -36,134 +36,202 @@ # @handy-common-utils/fs-utils

**[@handy-common-utils/fs-utils](#readmemd)**
@handy-common-utils/fs-utils
> Globals
## @handy-common-utils/fs-utils
### Index
### Table of contents
#### Classes
#### Modules
* [FsUtils](#classesfsutilsmd)
- [fs-utils](#modulesfs_utilsmd)
#### Variables
## Classes
* [addSurroundingInFile](#addsurroundinginfile)
* [afterString](#afterstring)
* [beforeString](#beforestring)
* [changeFileContent](#changefilecontent)
* [contentFilePath](#contentfilepath)
* [escapeRegExpReplacement](#escaperegexpreplacement)
* [filePath](#filepath)
* [matchPattern](#matchpattern)
* [replaceInFile](#replaceinfile)
* [replaceInFileWithFileContent](#replaceinfilewithfilecontent)
### Variables
<a name="classesfs_utilsfsutilsmd"></a>
#### addSurroundingInFile
[@handy-common-utils/fs-utils](#readmemd) / [fs-utils](#modulesfs_utilsmd) / FsUtils
• `Const` **addSurroundingInFile**: [addSurroundingInFile](#addsurroundinginfile) = FsUtils.addSurroundingInFile
### Class: FsUtils
___
[fs-utils](#modulesfs_utilsmd).FsUtils
#### afterString
#### Table of contents
• **afterString**: string
##### Constructors
___
- [constructor](#constructor)
#### beforeString
##### Methods
• **beforeString**: string
- [addSurroundingInFile](#addsurroundinginfile)
- [changeFileContent](#changefilecontent)
- [escapeRegExpReplacement](#escaperegexpreplacement)
- [replaceInFile](#replaceinfile)
- [replaceInFileWithFileContent](#replaceinfilewithfilecontent)
___
#### Constructors
#### changeFileContent
##### constructor
• `Const` **changeFileContent**: [changeFileContent](#changefilecontent) = FsUtils.changeFileContent
• **new FsUtils**()
___
#### Methods
#### contentFilePath
##### addSurroundingInFile
• **contentFilePath**: string
▸ `Static` **addSurroundingInFile**(`filePath`, `matchPattern`, `addBefore`, `addAfter`, `fileEncoding?`): `Promise`<`void`\>
___
Add surrounding content to the matching sections in the text file.
#### escapeRegExpReplacement
###### Parameters
• `Const` **escapeRegExpReplacement**: [escapeRegExpReplacement](#escaperegexpreplacement) = FsUtils.escapeRegExpReplacement
| 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
___
#### filePath
##### changeFileContent
• **filePath**: string
▸ `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
___
#### matchPattern
##### escapeRegExpReplacement
• **matchPattern**: string
▸ `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 '$'
___
#### replaceInFile
##### replaceInFile
• `Const` **replaceInFile**: [replaceInFile](#replaceinfile) = FsUtils.replaceInFile
▸ `Static` **replaceInFile**(`filePath`, `matchPattern`, `replacementOrBuilder`, `fileEncoding?`): `Promise`<`void`\>
Replace 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 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 |
###### Returns
`Promise`<`void`\>
Promise of void
___
#### replaceInFileWithFileContent
##### replaceInFileWithFileContent
• `Const` **replaceInFileWithFileContent**: [replaceInFileWithFileContent](#replaceinfilewithfilecontent) = FsUtils.replaceInFileWithFileContent
▸ `Static` **replaceInFileWithFileContent**(`filePath`, `matchPattern`, `contentFilePath`, `fileEncoding?`): `Promise`<`void`\>
## Classes
Replace the matching sections in the text file with content from another file.
###### Parameters
<a name="classesfsutilsmd"></a>
| 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 |
**[@handy-common-utils/fs-utils](#readmemd)**
###### Returns
> [Globals](#readmemd) / FsUtils
`Promise`<`void`\>
### Class: FsUtils
Promise of void
#### Hierarchy
## Modules
* **FsUtils**
#### Index
<a name="modulesfs_utilsmd"></a>
##### Methods
[@handy-common-utils/fs-utils](#readmemd) / fs-utils
* [addSurroundingInFile](#addsurroundinginfile)
* [changeFileContent](#changefilecontent)
* [escapeRegExpReplacement](#escaperegexpreplacement)
* [replaceInFile](#replaceinfile)
* [replaceInFileWithFileContent](#replaceinfilewithfilecontent)
### Module: fs-utils
#### Methods
#### Table of contents
##### Classes
- [FsUtils](#classesfs_utilsfsutilsmd)
##### Functions
- [addSurroundingInFile](#addsurroundinginfile)
- [changeFileContent](#changefilecontent)
- [escapeRegExpReplacement](#escaperegexpreplacement)
- [replaceInFile](#replaceinfile)
- [replaceInFileWithFileContent](#replaceinfilewithfilecontent)
#### Functions
##### addSurroundingInFile
▸ `Static` **addSurroundingInFile**(`filePath`: string, `matchPattern`: RegExp, `addBefore`: string, `addAfter`: string, `fileEncoding?`: Parameters\<Buffer[\"toString\"]>[\"0\"]): Promise\<void>
▸ `Const` **addSurroundingInFile**(`filePath`, `matchPattern`, `addBefore`, `addAfter`, `fileEncoding?`): `Promise`<`void`\>
Add surrounding content to the matching sections in the text file.
###### Parameters
###### Parameters:
| Name | Type | Default value |
| :------ | :------ | :------ |
| `filePath` | `string` | `undefined` |
| `matchPattern` | `RegExp` | `undefined` |
| `addBefore` | `string` | `undefined` |
| `addAfter` | `string` | `undefined` |
| `fileEncoding` | `undefined` \| `string` | `'utf-8'` |
Name | Type | Default value | Description |
------ | ------ | ------ | ------ |
`filePath` | string | - | path to the file |
`matchPattern` | RegExp | - | 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 | - | the string to be added before the capturing group, no need to escape anything |
`addAfter` | string | - | the string to be added before the capturing group, no need to escape anything |
`fileEncoding` | Parameters\<Buffer[\"toString\"]>[\"0\"] | "utf-8" | encoding of the file |
###### Returns
**Returns:** Promise\<void>
`Promise`<`void`\>
Promise of void
___

@@ -173,20 +241,16 @@

▸ `Static` **changeFileContent**(`filePath`: string, `transformContent`: (originalContent: string, filePath: string) => string \| PromiseLike\<string>, `fileEncoding?`: Parameters\<Buffer[\"toString\"]>[\"0\"]): Promise\<void>
▸ `Const` **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
###### Parameters:
| Name | Type | Default value |
| :------ | :------ | :------ |
| `filePath` | `string` | `undefined` |
| `transformContent` | (`originalContent`: `string`, `filePath`: `string`) => `string` \| `PromiseLike`<`string`\> | `undefined` |
| `fileEncoding` | `undefined` \| `string` | `'utf-8'` |
Name | Type | Default value | Description |
------ | ------ | ------ | ------ |
`filePath` | string | - | path to the file |
`transformContent` | (originalContent: string, filePath: string) => string \| PromiseLike\<string> | - | function for getting the new file content |
`fileEncoding` | Parameters\<Buffer[\"toString\"]>[\"0\"] | "utf-8" | encoding of the file |
###### Returns
**Returns:** Promise\<void>
`Promise`<`void`\>
Promise of void
___

@@ -196,18 +260,14 @@

▸ `Static` **escapeRegExpReplacement**(`input`: string): string
▸ `Const` **escapeRegExpReplacement**(`input`): `string`
Escape the '
sign in the string for using the string as the second argument to String.replace(...)
###### Parameters
###### Parameters:
| Name | Type |
| :------ | :------ |
| `input` | `string` |
Name | Type | Description |
------ | ------ | ------ |
`input` | string | the original string |
###### Returns
**Returns:** string
`string`
a new string with all '
in the original string being replaced by '$'
___

@@ -217,19 +277,17 @@

▸ `Static` **replaceInFile**(`filePath`: string, `matchPattern`: RegExp, `replacementOrBuilder`: string \| (matchPattern: RegExp, filePath: string) => string \| PromiseLike\<string>, `fileEncoding?`: Parameters\<Buffer[\"toString\"]>[\"0\"]): Promise\<void>
▸ `Const` **replaceInFile**(`filePath`, `matchPattern`, `replacementOrBuilder`, `fileEncoding?`): `Promise`<`void`\>
Replace the matching sections in the text file.
###### Parameters
###### Parameters:
| 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'` |
Name | Type | Default value | Description |
------ | ------ | ------ | ------ |
`filePath` | string | - | path to the file |
`matchPattern` | RegExp | - | 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> | - | 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` | Parameters\<Buffer[\"toString\"]>[\"0\"] | "utf-8" | encoding of the file |
###### Returns
**Returns:** Promise\<void>
`Promise`<`void`\>
Promise of void
___

@@ -239,18 +297,16 @@

▸ `Static` **replaceInFileWithFileContent**(`filePath`: string, `matchPattern`: RegExp, `contentFilePath`: string, `fileEncoding?`: Parameters\<Buffer[\"toString\"]>[\"0\"]): Promise\<void>
▸ `Const` **replaceInFileWithFileContent**(`filePath`, `matchPattern`, `contentFilePath`, `fileEncoding?`): `Promise`<`void`\>
Replace the matching sections in the text file with content from another file.
###### Parameters
###### Parameters:
| Name | Type | Default value |
| :------ | :------ | :------ |
| `filePath` | `string` | `undefined` |
| `matchPattern` | `RegExp` | `undefined` |
| `contentFilePath` | `string` | `undefined` |
| `fileEncoding` | `undefined` \| `string` | `'utf-8'` |
Name | Type | Default value | Description |
------ | ------ | ------ | ------ |
`filePath` | string | - | path of the file |
`matchPattern` | RegExp | - | 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 | - | path of the file for getting the replacement content |
`fileEncoding` | Parameters\<Buffer[\"toString\"]>[\"0\"] | "utf-8" | encoding of the files |
###### Returns
**Returns:** Promise\<void>
Promise of void
`Promise`<`void`\>
<!-- API end -->
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc