FileSystem

The FileSystem class is used to query and modify attributes of files and directories on disk. A shared instance of the class is always available as the nova.fs global object.

Properties

constants

An object containing a set of common constants used with file system operations.

These constants are exposed both within this object, and directly on the fs object itself.

This property is readonly.

Methods

access(path, modes)

Determines if the file at a specified path is accessible via the specified mode(s). If the path matches the modes, the return value will be true, otherwise false.

The modes argument is a bitfield created using the F_OK, R_OK, W_OK, and X_OK constants (see above).

copy(src, dest)

Copies a file at a source path to a destination path. If no file exists at the source path, or if a file already exists at the destination path, this will throw an Error.

copyAsync(src, dest, callback, [thisValue])

Copies a file at a source path to a destination path asynchronously. When the operation is complete, the callback will be called with an optional error argument (only if the operation failed). The optional thisValue argument can be used to bind a custom this within the invoked callback. If no file exists at the source path, or if a file already exists at the destination path, this will return an Error.

listdir(path)

Returns an array of paths listing the contents of the specified directory. If no directory exists at the path (or if it’s not a directory), this will throw an Error.

mkdir(path)

Creates a directory at path. If a file already exists at the path, this will throw an Error.

move(src, dest)

Moves a file at a source path to a destination path. If no file exists at the source path, or if a file already exists at the destination path, this will throw an Error.

moveAsync(src, dest, callback, [thisValue])

Moves a file at a source path to a destination path asynchronously. When the operation is complete, the callback will be called with an optional error argument (only if the operation failed). The optional thisValue argument can be used to bind a custom this within the invoked callback. If no file exists at the source path, or if a file already exists at the destination path, this will return an Error.

remove(path)

Removes a file at a path. This method is only valid for regular files. If the path represents a directory, this will throw an Error (use the rmdir() method instead). If no file exists at the path, this method does nothing.

rmdir(path)

Removes a directory at a path. This method is only valid for directories. If the path is not a directory, this will throw an Error. If no directory exists at the path, this method does nothing.

stat(path)

Returns information about the file at a path as a FileStats object. If no file exists at the path, this method returns null.

eject(path)

Ejects the disk at the provided path. If the path does not refer to a mounted volume, this method will throw an Error.

reveal(path)

Displays a Finder window and reveals the item represented by the provided path.

open(path, [mode], [encoding])

Opens a file from the specified path, creating and returning a File object.

The mode string argument specifies in what way the file is opened. It can contain the following components:

Value Description
r Open for reading (default)
w Open for writing, truncating the file first
x Open for exclusive creation, failing if the file exists
a Open for writing, appending to the end if it exists
b Binary mode
t Text mode (default)
+ Open for updating (reading and writing)

The default mode is 'r' (open for reading, synonym of ‘rt’). For binary read-write access, the mode 'w+b' opens and truncates the file to 0 bytes. 'r+b' opens the file in binary mode without truncation.

Files can be created in one of two modes: “Binary Mode” or “Text Mode”. In Binary mode, reading from the file will read TypedArray objects. In Text mode, the file object will attempt to interpret read data in a specific encoding (specified when the file was created) and return string objects. By default, the encoding of a file in text mode with no encoding specified is UTF-8.

For files opened in Text mode, the optional encoding argument can be used to set what encoding will be used to interpret read and (by default) written data. If this argument is not specified, UTF-8 will be used.

Supported encodings include:

watch(pattern, callable)

Creates a FileSystemWatcher object that observes changes to the file system relative to the workspace. The pattern is a glob pattern which specifies the files to observe. The pattern may be null to indicate that changes to all files and directories should be observed. The provided callable will be invoked when files are added, modified, or removed.