Class: File
Class: File
Class representing a File.
Author
Mr. Stone evensstone@gmail.com
Extended by
Constructors
Constructor
protected new File(path, checkPath): File;
Create a File.
Parameters
path
string
The file path.
checkPath
boolean
= true
Whether to check if the file path is valid.
Returns
File
Methods
edit()
edit(callback): this;
Edit file content.
Parameters
callback
(content
) => string
The callback function to modify the file content.
Returns
this
The current File instance.
exists()
exists(): boolean;
Check if the file exists.
Returns
boolean
True if the file exists, otherwise false.
getAbsolutePath()
getAbsolutePath(root): string;
Get the absolute file path.
Parameters
root
string
= ''
The root directory to resolve from.
Returns
string
The absolute file path.
getATime()
getATime(): undefined | number | bigint;
Get the last access time of the file.
Returns
undefined
| number
| bigint
The last access time in milliseconds.
getBasename()
getBasename(exclude): string;
Get the basename of the file.
Parameters
exclude
string
= ''
The file extension to exclude from the basename.
Returns
string
The basename of the file.
getContent()
getContent(encoding): string;
Get file content.
Parameters
encoding
BufferEncoding
= 'utf-8'
Returns
string
The content of the file as a string.
getCTime()
getCTime(): undefined | number | bigint;
Get the created time of the file.
Returns
undefined
| number
| bigint
The created time in milliseconds.
getDirname()
getDirname(): string;
Get the directory name of the file.
Returns
string
The directory name.
getEncodedAbsolutePath()
getEncodedAbsolutePath(root): string;
Get the encoded absolute file path.
Parameters
root
string
= ''
The root directory to resolve from.
Returns
string
The encoded absolute file path.
getEncodedPath()
getEncodedPath(): string;
Get the encoded file path.
Returns
string
The encoded file path.
getExtension()
getExtension(): string;
Get the file extension.
Returns
string
The file extension.
getFilename()
getFilename(): string;
Get the filename of the file.
Returns
string
The filename of the file.
getHashedContent()
getHashedContent(algo): string;
Get the hashed content of the file.
Parameters
algo
string
= 'sha256'
The hashing algorithm to use.
Returns
string
The hashed content of the file as a hex string.
getMimeType()
Get the MIME type of the file.
Param
A fallback MIME type if detection fails.
Call Signature
getMimeType(): undefined | string;
Get the MIME type of the file.
Returns
undefined
| string
The MIME type of the file.
Param
A fallback MIME type if detection fails.
Call Signature
getMimeType(fallback): string;
Get the MIME type of the file.
Parameters
fallback
string
A fallback MIME type if detection fails.
Returns
string
The MIME type of the file.
Param
A fallback MIME type if detection fails.
getMTime()
getMTime(): undefined | number | bigint;
Get the last modified time of the file.
Returns
undefined
| number
| bigint
The last modified time in milliseconds.
getName()
getName(): string;
Get the name of the file without extension.
Returns
string
The name of the file.
getPath()
getPath(): string;
Get the file path.
Returns
string
The file path.
getSize()
getSize(formatted): undefined | string | number | bigint;
Get file size.
Parameters
formatted
boolean
= false
Whether to return the file size as a formatted string.
Returns
undefined
| string
| number
| bigint
The file size as a string or number.
isAbsolute()
isAbsolute(): boolean;
Check if the file path is absolute.
Returns
boolean
True if the file path is absolute, otherwise false.
isCompressed()
isCompressed(extensions): boolean;
Check if the file is compressed.
Parameters
extensions
string
[] = ...
Returns
boolean
True if the file is compressed, otherwise false.
isDir()
isDir(): boolean;
Check if the file is a directory.
Returns
boolean
True if the file is a directory, otherwise false.
isExecutable()
isExecutable(): boolean;
Check if the file is executable.
Returns
boolean
True if the file is executable, otherwise false.
isFile()
isFile(): boolean;
Check if the file is a regular file.
Returns
boolean
True if the file is a regular file, otherwise false.
isLink()
isLink(): boolean;
Check if the file is a symbolic link.
Returns
boolean
True if the file is a symbolic link, otherwise false.
isPathPrefix()
isPathPrefix(prefix): boolean;
Check if the file path starts with a provided prefix.
Parameters
prefix
string
Returns
boolean
True if the file path starts with the provided prefix, otherwise false.
isReadable()
isReadable(): boolean;
Check if the file is readable.
Returns
boolean
True if the file is readable, otherwise false.
isWritable()
isWritable(): boolean;
Check if the file is writable.
Returns
boolean
True if the file is writable, otherwise false.
move()
move(directory, name?): File;
Move file to a new directory.
Parameters
directory
string
The target directory.
name?
string
The new name for the file.
Returns
File
The new File instance representing the moved file.
Throws
FileError if the file could not be moved.
remove()
remove(force): this;
Remove file.
Parameters
force
boolean
= false
Whether to forcefully remove the file.
Returns
this
The current File instance.
write()
write(content): this;
Write content to file.
Parameters
content
string
The content to write to the file.
Returns
this
The current File instance.
create()
static create(path, checkPath): File;
Create a File.
Parameters
path
string
The file path.
checkPath
boolean
= true
Whether to check if the file path is valid.
Returns
File
A new File instance.