Package Exports
- @handy-common-utils/fs-utils
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@handy-common-utils/fs-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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-utilsThen you can use it in the code:
import { FsUtils } from '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
Globals
@handy-common-utils/fs-utils
Index
Classes
Variables
- addSurroundingInFile
- afterString
- beforeString
- changeFileContent
- contentFilePath
- escapeRegExpReplacement
- filePath
- matchPattern
- replaceInFile
- replaceInFileWithFileContent
Variables
addSurroundingInFile
• Const addSurroundingInFile: addSurroundingInFile = FsUtils.addSurroundingInFile
afterString
• afterString: string
beforeString
• beforeString: string
changeFileContent
• Const changeFileContent: changeFileContent = FsUtils.changeFileContent
contentFilePath
• contentFilePath: string
escapeRegExpReplacement
• Const escapeRegExpReplacement: escapeRegExpReplacement = FsUtils.escapeRegExpReplacement
filePath
• filePath: string
matchPattern
• matchPattern: string
replaceInFile
• Const replaceInFile: replaceInFile = FsUtils.replaceInFile
replaceInFileWithFileContent
• Const replaceInFileWithFileContent: replaceInFileWithFileContent = FsUtils.replaceInFileWithFileContent
Classes
Globals / FsUtils
Class: FsUtils
Hierarchy
- FsUtils
Index
Methods
- addSurroundingInFile
- changeFileContent
- escapeRegExpReplacement
- replaceInFile
- replaceInFileWithFileContent
Methods
addSurroundingInFile
▸ Static addSurroundingInFile(filePath: string, matchPattern: RegExp, addBefore: string, addAfter: string, fileEncoding?: Parameters<Buffer["toString"]>["0"]): Promise<void>
Add surrounding content to the matching sections in the text file.
Parameters:
| 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: Promise<void>
changeFileContent
▸ Static changeFileContent(filePath: string, transformContent: (originalContent: string, filePath: string) => string | PromiseLike<string>, fileEncoding?: Parameters<Buffer["toString"]>["0"]): 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 | - | 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: Promise<void>
escapeRegExpReplacement
▸ Static escapeRegExpReplacement(input: string): 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
▸ Static replaceInFile(filePath: string, matchPattern: RegExp, replacementOrBuilder: string | (matchPattern: RegExp, filePath: string) => string | PromiseLike<string>, fileEncoding?: Parameters<Buffer["toString"]>["0"]): Promise<void>
Replace the matching sections in the text file.
Parameters:
| 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: Promise<void>
replaceInFileWithFileContent
▸ Static replaceInFileWithFileContent(filePath: string, matchPattern: RegExp, contentFilePath: string, fileEncoding?: Parameters<Buffer["toString"]>["0"]): Promise<void>
Replace the matching sections in the text file with content from another file.
Parameters:
| 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: Promise<void>