1.1.1 • Published 11 months ago

@handy-common-utils/fs-utils v1.1.1

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
11 months ago

@handy-common-utils/fs-utils

File system operations related utilities without any 3rd party dependency.

Version Downloads/week CI codecov

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>');

There are also several commands you can use directly from your shell/build scripts:

  • replace-in-file filePath matchPattern replacement
  • replace-in-files matchPattern replacement file1 file2 file3 ...
  • replace-in-file-with-file-content filePath matchPattern contentFilePath
  • add-surrounding-in-file filePath matchPattern beforeString afterString

API

Module: fs-utils

Re-exports

Functions

Exports

Classes

Type Aliases

FileEncoding

Ƭ FileEncoding: Parameters<Buffer"toString">"0"


ReplacementOrBuilder

Ƭ ReplacementOrBuilder: string | (matchPattern: RegExp, filePath: string) => string | PromiseLike<string>

Classes

Class: FsUtils

fs-utils.FsUtils

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
NameTypeDefault valueDescription
filePathstringundefinedpath to the file
matchPatternRegExpundefinedRegExp 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]*, .*?
addBeforestringundefinedthe string to be added before the capturing group, no need to escape anything
addAfterstringundefinedthe string to be added before the capturing group, no need to escape anything
fileEncodingundefined | BufferEncoding'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
NameTypeDefault valueDescription
filePathstringundefinedpath to the file
transformContent(originalContent: string, filePath: string) => string | PromiseLike<string>undefinedfunction for getting the new file content
fileEncodingundefined | BufferEncoding'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
NameTypeDescription
inputstringthe original string
Returns

string

a new string with all ' in the original string being replaced by '$'


replaceInFile

Static replaceInFile(filePath, matchPattern, replacementOrBuilder, fileEncoding?): Promise<void>

Replace the matching sections in the text file.

Parameters
NameTypeDefault valueDescription
filePathstringundefinedpath to the file
matchPatternRegExpundefinedRegExp 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]*, .*?
replacementOrBuilderReplacementOrBuilderundefinedThe 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
fileEncodingundefined | BufferEncoding'utf-8'encoding of the file
Returns

Promise<void>

Promise of void


replaceInFileWithFileContent

Static replaceInFileWithFileContent(filePath, matchPattern, contentFilePath, fileEncoding?): Promise<void>

Replace the matching sections in the text file with content from another file.

Parameters
NameTypeDefault valueDescription
filePathstringundefinedpath of the file
matchPatternRegExpundefinedRegExp 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]*, .*?
contentFilePathstringundefinedpath of the file for getting the replacement content
fileEncodingundefined | BufferEncoding'utf-8'encoding of the files
Returns

Promise<void>

Promise of void


replaceInFiles

Static replaceInFiles(matchPattern, replacementOrBuilder, ...filePaths): Promise<void>

Replace the matching sections in multiple utf-8 text files. The replacing opertions on those files happen in parallel.

Parameters
NameTypeDescription
matchPatternRegExpRegExp 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]*, .*?
replacementOrBuilderReplacementOrBuilderThe 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
...filePathsstring[]patsh to the files
Returns

Promise<void>

Promise of void


replaceInFilesWithEncoding

Static replaceInFilesWithEncoding(matchPattern, replacementOrBuilder, fileEncoding, ...filePaths): Promise<void>

Replace the matching sections in multiple text files. The replacing opertions on those files happen in parallel.

Parameters
NameTypeDescription
matchPatternRegExpRegExp 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]*, .*?
replacementOrBuilderReplacementOrBuilderThe 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
fileEncodingundefined | BufferEncodingencoding of the file, in most of the cases 'utf-8' should be used
...filePathsstring[]patsh to the files
Returns

Promise<void>

Promise of void

1.1.1

11 months ago

1.1.0

12 months ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.20

1 year ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.5

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago