@xan105/fs v2.3.0
About
File system manipulation without headache.
š¦ Scoped @xan105 packages are for my own personal use but feel free to use them.
Install / Runtime
š¦ Package manager
npm install @xan105/fs- Node āļø
API
ā ļø This module is only available as an ECMAScript module (ESM) starting with version 2.0.0. Previous version(s) are CommonJS (CJS) with an ESM wrapper.
Named export
readFile(filePath: string, options?: object | string): Promise<string | Buffer>
Auto remove utf bom (string only).
readJSON(filePath: string): Promise<string>
Parse JSON and remove utf bom if any.
writeFile(filePath: string, data: unknown, options?: object | string): Promise<string>
Create target parent dir if doesn't exist.
āļø Options:
bom(default: false)add utf bom (string only).
writeJSON(filePath: string, data: unknown, option?: object): Promise<string>
Write JSON to file and return filePath for convenience.
āļø Options:
pretty(default: true)insert white space into the output JSON string for readability purposes.
bigint2str(default: true)convert bigint to string.
copyFile(src: string, dest: string, flags: number): Promise<void>
Create target parent dir if doesn't exist.
mv(oldPath: string, newPath: string): Promise<string>
Create target parent dir if doesn't exist. Move on same drive otherwise copy to target and delete origin.
exists(path: string): Promise<boolean>
existsAndIsOlderOrYoungerThan(path: string, option?: object): Promise<boolean>
āļø Options:
time: amount of time unit (default 1)timeUnit: s|m|h|d|w|M|Y (default day)younger: compare mode younger than (true) or older than (false/default)
existsAndIsOlderThan(path: string, option?: object): Promise<boolean>
āļø Options:
time: amount of time unit (default 1)timeUnit: s|m|h|d|w|M|Y (default day)
existsAndIsYoungerThan(path: string, option?: object): Promise<boolean>
āļø Options:
time: amount of time unit (default 1)timeUnit: s|m|h|d|w|M|Y (default day))
stat(path: string): Promise<object>
alias stats(path: string): Promise<object>
On error returns an empty object.
mkdir(dirPath: string): Promise<string | undefined>
Recursive.
rm(path: string): Promise<void>
alias unlink(path: string): Promise<void>
alias deleteFile(path: string): Promise<void>
alias rmdir(path: string): Promise<void>
Nuke the target šš„.
hashFile(filePath: string, algo?: string): Promise<string>
Hash specified file (use stream). algo defaults to "sha256".
touch(filePath: string): Promise<void>
Create, change and modify timestamps of a file.
ls(dirPath: string, option?: object): Promise<string[] | object[]>
List directory contents.
āļø Options:
verbose: boolean (false)recursive: boolean (false)absolute: boolean false)Return absolute or relative path
follow: boolean (false)Whether or not to follow symlink
normalize: boolean (false)Whether or not to normalize path seperator to "/"
ignore:dir: boolean (false)file: boolean (false)symlink: boolean (false)socket: boolean (false)dot: boolean (true)
whitelist: boolean (false)Turn the filter list into a whitelist
Allowed file extension
pattern: RegExp (none)Return value:
If
verboseistruereturns a detailed list as
[
{
name: string, //file name
path: string, //path of file
link?: string | undefined //symlink target
},
...
]Otherwise string[] (path of file)
compareFile(a: string, b:string, algo?: string): Promise<boolean>
Compare files to determine if they are identical.
Comparison is by default done with "sha1" algo (cf: hashFile()) unless files size don't match.
path
resolve(path: string): string
Resolve fileURL and path if necessary.
dirname(path: string): string
Handles path and fileURL.
normalize(path: string, win32?: boolean): string
replace every \\ with /
When win32 is set to true (default is false)
replace every / with \\