Skip to main content

API Reference

Classes

Fs

The fs class is used for interacting with the file system.

All file paths must be POSIX file paths (/ instead of ), and will be normalized to the target platform if running on Windows.

Initializers

bring fs;

new fs.Util();
NameTypeDescription

Static Functions

NameDescription
absoluteThe right-most parameter is considered {to}. Other parameters are considered an array of {from}.
basenameRetrieve the final segment of a given file path.
dirnameRetrieve the name of the directory from a given file path.
existsCheck if the path exists.
extensionExtracts the extension (without the leading dot) from the path, if possible.
isDirChecks if the given path is a directory and exists.
joinJoin all arguments together and normalize the resulting path.
metadataGets the stats of the given path.
mkdirCreate a directory.
mkdtempCreate a temporary directory.
readdirRead the contents of the directory.
readFileRead the entire contents of a file.
readJsonRead the contents of the file and convert it to JSON.
readYamlConvert all YAML objects from a single file into JSON objects.
relativeSolve the relative path from {from} to {to} based on the current working directory.
removeRemove files and directories (modeled on the standard POSIX rmutility).
setPermissionsSet the permissions of the file, directory, etc.
symlinkCreates a symbolic link.
symlinkMetadataGets the stats of the given path without following symbolic links.
tryReaddirIf the path exists, read the contents of the directory;
tryReadFileIf the file exists and can be read successfully, read the entire contents;
tryReadJsonRetrieve the contents of the file and convert it to JSON if the file exists and can be parsed successfully, otherwise, return undefined.
tryReadYamlConvert all YAML objects from a single file into JSON objects if the file exists and can be parsed successfully, undefined otherwise.
writeFileWrites data to a file, replacing the file if it already exists.
writeJsonWrites JSON to a file, replacing the file if it already exists.
writeYamlWrites multiple YAML objects to a file, replacing the file if it already exists.

absolute
bring fs;

fs.absolute(...paths: Array<str>);

The right-most parameter is considered {to}. Other parameters are considered an array of {from}.

Starting from leftmost {from} parameter, resolves {to} to an absolute path.

If {to} isn't already absolute, {from} arguments are prepended in right to left order, until an absolute path is found. If after using all {from} paths still no absolute path is found, the current working directory is used as well. The resulting path is normalized, and trailing slashes are removed unless the path gets resolved to the root directory.

pathsRequired
  • Type: str

A sequence of paths or path segments.


basename
bring fs;

fs.basename(path: str);

Retrieve the final segment of a given file path.

pathRequired
  • Type: str

The path to evaluate.


dirname
bring fs;

fs.dirname(path: str);

Retrieve the name of the directory from a given file path.

pathRequired
  • Type: str

The path to evaluate.


exists
bring fs;

fs.exists(path: str);

Check if the path exists.

pathRequired
  • Type: str

The path to evaluate.


extension
bring fs;

fs.extension(path: str);

Extracts the extension (without the leading dot) from the path, if possible.

pathRequired
  • Type: str

The path to get extension for.


isDir
bring fs;

fs.isDir(path: str);

Checks if the given path is a directory and exists.

pathRequired
  • Type: str

The path to check.


join
bring fs;

fs.join(...paths: Array<str>);

Join all arguments together and normalize the resulting path.

pathsRequired
  • Type: str

The array of path need to join.


metadata
bring fs;

fs.metadata(path: str);

Gets the stats of the given path.

pathRequired
  • Type: str

The path to get stats for.


mkdir
bring fs;

fs.mkdir(dirpath: str, opts?: MkdirOptions);

Create a directory.

dirpathRequired
  • Type: str

The path to the directory you want to create.


optsOptional

mkdtemp
bring fs;

fs.mkdtemp(prefix?: str);

Create a temporary directory.

Generates six random characters to be appended behind a required prefix to create a unique temporary directory.

prefixOptional
  • Type: str

The prefix for the directory to be created, default wingtemp.


readdir
bring fs;

fs.readdir(dirpath: str);

Read the contents of the directory.

dirpathRequired
  • Type: str

The path to evaluate.


readFile
bring fs;

fs.readFile(filepath: str, options?: ReadFileOptions);

Read the entire contents of a file.

filepathRequired
  • Type: str

The path of the file to be read.


optionsOptional

The encoding can be set to specify the character encoding.

And the flag can be set to specify the attributes. If a flag is not provided, it defaults to "r".


readJson
bring fs;

fs.readJson(filepath: str);

Read the contents of the file and convert it to JSON.

filepathRequired
  • Type: str

The file path of the JSON file.


readYaml
bring fs;

fs.readYaml(filepath: str);

Convert all YAML objects from a single file into JSON objects.

filepathRequired
  • Type: str

The file path of the YAML file.


relative
bring fs;

fs.relative(from: str, to: str);

Solve the relative path from {from} to {to} based on the current working directory.

At times we have two absolute paths, and we need to derive the relative path from one to the other.

fromRequired
  • Type: str

toRequired
  • Type: str

remove
bring fs;

fs.remove(path: str, opts?: RemoveOptions);

Remove files and directories (modeled on the standard POSIX rmutility).

Returns undefined.

pathRequired
  • Type: str

The path to the file or directory you want to remove.


optsOptional

setPermissions
bring fs;

fs.setPermissions(path: str, permissions: str);

Set the permissions of the file, directory, etc.

Expects a permission string like "755" or "644".

pathRequired
  • Type: str

The path of the file or directory.


permissionsRequired
  • Type: str

The mode to set as a string.


bring fs;

fs.symlink(target: str, path: str, type?: SymlinkType);

Creates a symbolic link.

targetRequired
  • Type: str

The path to the target file or directory.


pathRequired
  • Type: str

The path to the symbolic link to be created.


typeOptional

The type of the target.

It can be FILE, DIRECTORY, or JUNCTION (Windows only). Defaults to FILE if not specified.


symlinkMetadata
bring fs;

fs.symlinkMetadata(path: str);

Gets the stats of the given path without following symbolic links.

pathRequired
  • Type: str

The path to get stats for.


tryReaddir
bring fs;

fs.tryReaddir(dirpath: str);

If the path exists, read the contents of the directory;

otherwise, return undefined.

dirpathRequired
  • Type: str

The path to evaluate.


tryReadFile
bring fs;

fs.tryReadFile(filepath: str, options?: ReadFileOptions);

If the file exists and can be read successfully, read the entire contents;

otherwise, return undefined.

filepathRequired
  • Type: str

The path of the file to be read.


optionsOptional

The encoding can be set to specify the character encoding, or the flag can be set to specify the attributes.


tryReadJson
bring fs;

fs.tryReadJson(filepath: str);

Retrieve the contents of the file and convert it to JSON if the file exists and can be parsed successfully, otherwise, return undefined.

filepathRequired
  • Type: str

The file path of the JSON file.


tryReadYaml
bring fs;

fs.tryReadYaml(filepath: str);

Convert all YAML objects from a single file into JSON objects if the file exists and can be parsed successfully, undefined otherwise.

filepathRequired
  • Type: str

The file path of the YAML file.


writeFile
bring fs;

fs.writeFile(filepath: str, data: str, options?: WriteFileOptions);

Writes data to a file, replacing the file if it already exists.

filepathRequired
  • Type: str

The file path that needs to be written.


dataRequired
  • Type: str

The data to write.


optionsOptional

The encoding can be set to specify the character encoding.

And the flag can be set to specify the attributes. If a flag is not provided, it defaults to "w".


writeJson
bring fs;

fs.writeJson(filepath: str, obj: Json);

Writes JSON to a file, replacing the file if it already exists.

filepathRequired
  • Type: str

The file path that needs to be written.


objRequired

The JSON object to be dumped.


writeYaml
bring fs;

fs.writeYaml(filepath: str, ...objs: Array<Json>);

Writes multiple YAML objects to a file, replacing the file if it already exists.

filepathRequired
  • Type: str

The file path that needs to be written.


objsRequired

The YANL objects to be dumped.


Structs

Metadata

Metadata of a file system object.

Initializer

bring fs;

let Metadata = fs.Metadata{ ... };

Properties

NameTypeDescription
accesseddatetimeThe date and time the file was last accessed.
createddatetimeThe date and time the file was created.
fileTypeFileTypeThe type of file.
modifieddatetimeThe date and time the file was last modified.
permissionsstrThe permissions of the file.
sizenumThe size of the file in bytes.

accessedRequired
accessed: datetime;

The date and time the file was last accessed.


createdRequired
created: datetime;

The date and time the file was created.


fileTypeRequired
fileType: FileType;

The type of file.


modifiedRequired
modified: datetime;

The date and time the file was last modified.


permissionsRequired
permissions: str;
  • Type: str

The permissions of the file.


sizeRequired
size: num;
  • Type: num

The size of the file in bytes.


MkdirOptions

Custom settings for creating directory.

Initializer

bring fs;

let MkdirOptions = fs.MkdirOptions{ ... };

Properties

NameTypeDescription
modestrA file mode.
recursiveboolIndicates whether parent folders should be created.

modeOptional
mode: str;
  • Type: str
  • Default: "0777"

A file mode.

The string will be parsed as an octal integer.


recursiveOptional
recursive: bool;
  • Type: bool
  • Default: true

Indicates whether parent folders should be created.

If a folder was created, the path to the first created folder will be returned.


ReadFileOptions

Custom settings for reading from a file.

Initializer

bring fs;

let ReadFileOptions = fs.ReadFileOptions{ ... };

Properties

NameTypeDescription
encodingstrThe character encoding utilized for file reading.
flagstrThe flag can be set to specify the attributes.

encodingOptional
encoding: str;
  • Type: str
  • Default: "utf-8"

The character encoding utilized for file reading.


flagOptional
flag: str;
  • Type: str
  • Default: "r".

The flag can be set to specify the attributes.


RemoveOptions

Custom settings for removing files and directories.

Initializer

bring fs;

let RemoveOptions = fs.RemoveOptions{ ... };

Properties

NameTypeDescription
forceboolWhen true, exceptions will be ignored if path does not exist.
recursiveboolIf true, perform a recursive directory removal.

forceOptional
force: bool;
  • Type: bool
  • Default: true

When true, exceptions will be ignored if path does not exist.


recursiveOptional
recursive: bool;
  • Type: bool
  • Default: true

If true, perform a recursive directory removal.

In recursive mode, operations are retried on failure.


WriteFileOptions

Custom settings for writing to a file.

Initializer

bring fs;

let WriteFileOptions = fs.WriteFileOptions{ ... };

Properties

NameTypeDescription
encodingstrThe character encoding utilized for file writing.
flagstrThe flag can be set to specify the attributes.

encodingOptional
encoding: str;
  • Type: str
  • Default: "utf-8"

The character encoding utilized for file writing.


flagOptional
flag: str;
  • Type: str
  • Default: "w".

The flag can be set to specify the attributes.


Enums

FileType

Represents the type of a file system object.

Members

NameDescription
FILERepresents a regular file.
DIRECTORYRepresents a directory.
SYMLINKRepresents a symbolic link.
OTHERRepresents any type of file system object that is not FILE, DIRECTORY or SYMLINK.

FILE

Represents a regular file.


DIRECTORY

Represents a directory.


Represents a symbolic link.


OTHER

Represents any type of file system object that is not FILE, DIRECTORY or SYMLINK.

This includes sockets, FIFOs (named pipes), block devices, and character devices.


SymlinkType

Represents the type of the target for creating symbolic links.

Members

NameDescription
FILESymbolic link that points to a file.
DIRECTORYSymbolic link that points to a directory.
JUNCTIONWindows-specific: Symbolic link that points to a directory junction.

FILE

Symbolic link that points to a file.


DIRECTORY

Symbolic link that points to a directory.


JUNCTION

Windows-specific: Symbolic link that points to a directory junction.