node-lib is a package that abstracts many internal NodeJS functions into user-friendly, intuitive class subdivisions. With node-lib, things like file and directory path handling, and binary data writing becomes easier to do!
FilePath|DirPath: Handle file and directory paths easily with built-in handling methods to read, write, copy, modify, and others.BinaryReader|BinaryWriter|StreamWriter: Parse and write binary files easily with several format parsing and writing methods.HexStr: Processes hex string with ease.MyObject: Create object maps with type assertion, with built-in methods to convert them to JavaScript object or serialized JSON string.
FilePath makes file path handling easy as it could be!
You can initialize a FilePath instance with the class constructor
import { FilePath } from 'node-lib'
const path = 'path/to/file.bin'
const file = new FilePath(path)or using the static method FilePath.of().
import { FilePath } from 'node-lib'
const path = 'path/to/file.bin'
const file = FilePath.of(path)FilePath accepts both absolute and relative paths. Relative paths will be resolved from the project working directory your initial script will be called upon.
| Property | Description |
|---|---|
| path | The working path of this class instance. |
| root | The root directory of the file where the path evaluates to. |
| fullname | The name of the file with extension (if any). |
| name | The name of the file (without the extension). |
| ext | The extension of the file (if any), returns an empty string if the provided path accidentally evalutes to a directory. |
You can also retrieve all these properties at once as an object using FilePath.toJSON() method.
You can get systems stats of a file as an object using the FilePath.stat() (async) or the FilePath.statSync() (sync) method.
changeFileName(fileName: string | null, fileExt?: string)— Changes the file name of thisFilePathand returns a new instantiatedFilePathwith the new file name.changeThisFileName(fileName: string | null, fileExt?: string)— Changes the file name of thisFilePathinstance.
changeFileExt(fileExt: string)— Changes the file extension of thisFilePathand returns a new instantiatedFilePathwith the new file extension.changeThisFileExt(fileExt: string)— Changes the file extension of thisFilePathinstance.
gotoDir(directoryName: string)— Returns a new instantiatedDirPath, resolving the path to a new directory relative from this file root path.gotoFile(fileName: string)— Returns a new instantiatedFilePath, resolving the path to a new file relative from this file root path.
You can use the property FilePath.exists to check the file existence. FilePath.exists is a getter that will always check once it's referenced.
import { FilePath } from 'node-lib'
const path: string = 'path/to/file.bin'
const file: FilePath = FilePath.of(path)
console.log(file.exists) // <- true
await file.deleteFile()
console.log(file.exists) // <- falseopen(flags?: string | number)— Asynchronously opens aFileHandle.
read(encoding?: BufferEncodingOrNull)— Reads the entire contents of a file. Returns aBufferor a string depending on the encoding provided.readSync(encoding?: BufferEncodingOrNull)— Synchronous version ofread().
readLines(options?: ReadLinesOptions)— Reads a file as a list of lines. Automatically trims and splits the file content on newline characters. Assumes the file content is text (not binary).readLinesSync(options?: ReadLinesOptions)— Synchronous version ofreadLines().
readJSON(encoding?: BufferEncodingText)— Reads a JSON file and parses it into an object. Attempts to decode the content using the provided encoding, then parses it usingJSON.parse.readJSONSync(encoding?: BufferEncodingText)— Synchronous version ofreadJSON().
readOffset(byteOffset: number, byteLength?: number)— Reads a portion of a file starting from a specific byte offset. IfbyteLengthis provided, reads that many bytes using a file descriptor. Otherwise, reads the whole file and returns a subarray starting at the offset.
write(data: FileAsyncWriteDataTypes, encoding?: BufferEncodingOrNull, replace?: boolean)— Writes data to a file, optionally replacing it if it already exists. Supports writing strings or buffers to a file with optional encoding. Throws an error if the file exists andreplaceis set tofalse.writeSync(data: FileSyncWriteDataTypes, encoding?: BufferEncodingOrNull, replace?: boolean)— Synchronous version ofwrite().
writeWithBOM(data: StringOrBuffer, encoding?: BufferEncodingBOM, replace?: boolean)— Writes a UTF-8 or UTF-16 encoded text file with a Byte Order Mark (BOM). Automatically prepends a BOM to the content. If encoding is not specified or is a variant of UTF-8, it uses UTF-8 with BOM. If encoding is'utf16le', it writes the content using UTF-16LE. Throws an error if the file exists andreplaceis set tofalse.writeWithBOMSync(data: StringOrBuffer, encoding?: BufferEncodingBOM, replace?: boolean)— Synchronous version ofwriteWithBOM().
createWriteStream(encoding?: BufferEncodingOrNull)— Creates a writable file stream at the specified path. If a file already exists at the path, it will be deleted before creating the stream. Optionally accepts an encoding; ifnullis passed, defaults toutf8.createWriteStreamSync(encoding?: BufferEncodingOrNull)— Synchronous version ofcreateWriteStream().
pipe<T>(source: PipelineSource<T>, options?: PipelineOptions)— Pipes the given source stream or iterable into an internal writable stream and returns a promise that resolves when the piping is complete or rejects on error.
-
copy(destPath: FilePathLikeTypes, replace?: boolean)— Copies a file from a source path to a destination path. If the destination path already exists:- Throws an error unless
replaceis set totrue. - If
replaceistrue, deletes the existing file before copying.
Automatically resolves relative destination paths based on the source file's directory.
- Throws an error unless
-
copySync(destPath: FilePathLikeTypes, replace?: boolean)— Synchronous version ofcopy().
-
rename(newPath: FilePathLikeTypes, replace?: boolean)— Renames (or moves) a file from an old path to a new path. If the new path already exists:- Throws an error unless
replaceistrue. - If
replaceistrue, deletes the destination file before renaming.
Automatically resolves relative
newPathvalues based on the directory of theoldPath. - Throws an error unless
-
renameSync(newPath: FilePathLikeTypes, replace?: boolean)— Synchronous version ofcopy(). -
move(newPath: FilePathLikeTypes, replace?: boolean)— Alias torename(). -
moveSync(newPath: FilePathLikeTypes, replace?: boolean)— Alias torenameSync().
delete()— Deletes a file at the specified path if it exists. Resolves the given path and performs a safe check before deletion to avoid errors.deleteSync()— Synchronous version ofdelete().remove()— Alias todelete().removeSync()— Alias todeleteSync().
generateHash(algorithm: AllHashAlgorithms, digest: BinaryToTextEncoding)— Asynchronously computes a cryptographic hash from the contents of the file.
You can initialize a DirPath instance with the class constructor
import { DirPath } from 'node-lib'
const path = 'path/to/directory'
const dir = new DirPath(path)or using the static method DirPath.of().
import { DirPath } from 'node-lib'
const path = 'path/to/directory'
const dir = DirPath.of(path)DirPath accepts both absolute and relative paths. Relative paths will be resolved from the project working directory your initial script will be called upon.
| Property | Description |
|---|---|
| path | The working path of this class instance. |
| root | The root directory of the file where the path evaluates to. |
| name | The name of the file (without the extension). |
You can also retrieve all these properties at once as an object using DirPath.toJSON() method.
You can get systems stats of a directory as an object using the DirPath.stat() (async) or the DirPath.statSync() (sync) method.
changeDirName(dirName: string)— Changes the directory name of thisDirPathand returns a new instantiatedDirPathwith the new directory name.changeThisDirName(dirName: string)— Changes the directory name of thisDirPathinstance.
gotoDir(directoryName: string)— Returns a new instantiatedDirPath, resolving the path to a new directory relative from this directory path.gotoFile(fileName: string)— Returns a new instantiatedDirPath, resolving the path to a new directory relative from this directory path.
You can use the property DirPath.exists to check the directory existence. DirPath.exists is a getter that will always check once it's referenced.
import { DirPath } from 'node-lib'
const path = 'path/to/directory'
const dir = DirPath.of(path)
console.log(dir.exists) // <- true
await dir.deleteDir()
console.log(dir.exists) // <- false