Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/node-core-library > FileSystem

FileSystem class

The FileSystem API provides a complete set of recommended operations for interacting with the file system.

Signature:

export declare class FileSystem 

Remarks

We recommend to use this instead of the native fs API, because fs is a minimal set of low-level primitives that must be mapped for each supported operating system. The FileSystem API takes a philosophical approach of providing "one obvious way" to do each operation. We also prefer synchronous operations except in cases where there would be a clear performance benefit for using async, since synchronous code is much easier to read and debug. Also, indiscriminate parallelism has been seen to actually worsen performance, versus improving it.

Note that in the documentation, we refer to "filesystem objects", this can be a file, folder, symbolic link, hard link, directory junction, etc.

Methods

Method

Modifiers

Description

appendToFile(filePath, contents, options)

static

Writes a text string to a file on disk, appending to the file if it already exists. Behind the scenes it uses fs.appendFileSync().

appendToFileAsync(filePath, contents, options)

static

An async version of FileSystem.appendToFile().

changePosixModeBits(path, modeBits)

static

Changes the permissions (i.e. file mode bits) for a filesystem object. Behind the scenes it uses fs.chmodSync().

changePosixModeBitsAsync(path, mode)

static

An async version of FileSystem.changePosixModeBits().

copyFile(options)

static

Copies a single file from one location to another. By default, destinationPath is overwritten if it already exists.

copyFileAsync(options)

static

An async version of FileSystem.copyFile().

copyFiles(options)

static

Copies a file or folder from one location to another, recursively copying any folder contents. By default, destinationPath is overwritten if it already exists.

copyFilesAsync(options)

static

An async version of FileSystem.copyFiles().

createHardLink(options)

static

Creates a hard link. The link target must be a file, not a folder. Behind the scenes it uses fs.linkSync().

createHardLinkAsync(options)

static

An async version of FileSystem.createHardLink().

createSymbolicLinkFile(options)

static

Creates a symbolic link to a file. On Windows operating systems, this may require administrator elevation. Behind the scenes it uses fs.symlinkSync().

createSymbolicLinkFileAsync(options)

static

An async version of FileSystem.createSymbolicLinkFile().

createSymbolicLinkFolder(options)

static

Creates a symbolic link to a folder. On Windows operating systems, this may require administrator elevation. Behind the scenes it uses fs.symlinkSync().

createSymbolicLinkFolderAsync(options)

static

An async version of FileSystem.createSymbolicLinkFolder().

createSymbolicLinkJunction(options)

static

Creates an NTFS "directory junction" on Windows operating systems; for other operating systems, it creates a regular symbolic link. The link target must be a folder, not a file. Behind the scenes it uses fs.symlinkSync().

createSymbolicLinkJunctionAsync(options)

static

An async version of FileSystem.createSymbolicLinkJunction().

deleteFile(filePath, options)

static

Deletes a file. Can optionally throw if the file doesn't exist. Behind the scenes it uses fs.unlinkSync().

deleteFileAsync(filePath, options)

static

An async version of FileSystem.deleteFile().

deleteFolder(folderPath)

static

Deletes a folder, including all of its contents. Behind the scenes is uses fs-extra.removeSync().

deleteFolderAsync(folderPath)

static

An async version of FileSystem.deleteFolder().

ensureEmptyFolder(folderPath)

static

Deletes the content of a folder, but not the folder itself. Also ensures the folder exists. Behind the scenes it uses fs-extra.emptyDirSync().

ensureEmptyFolderAsync(folderPath)

static

An async version of FileSystem.ensureEmptyFolder().

ensureFolder(folderPath)

static

Recursively creates a folder at a given path. Behind the scenes is uses fs-extra.ensureDirSync().

ensureFolderAsync(folderPath)

static

An async version of FileSystem.ensureFolder().

exists(path)

static

Returns true if the path exists on disk. Behind the scenes it uses fs.existsSync().

existsAsync(path)

static

An async version of FileSystem.exists().

formatPosixModeBits(modeBits)

static

Returns a 10-character string representation of a PosixModeBits value similar to what would be displayed by a command such as "ls -l" on a POSIX-like operating system.

getLinkStatistics(path)

static

Gets the statistics of a filesystem object. Does NOT follow the link to its target. Behind the scenes it uses fs.lstatSync().

getLinkStatisticsAsync(path)

static

An async version of FileSystem.getLinkStatistics().

getPosixModeBits(path)

static

Retrieves the permissions (i.e. file mode bits) for a filesystem object. Behind the scenes it uses fs.chmodSync().

getPosixModeBitsAsync(path)

static

An async version of FileSystem.getPosixModeBits().

getRealPath(linkPath)

static

Follows a link to its destination and returns the absolute path to the final target of the link. Behind the scenes it uses fs.realpathSync().

getRealPathAsync(linkPath)

static

An async version of FileSystem.getRealPath().

getStatistics(path)

static

Gets the statistics for a particular filesystem object. If the path is a link, this function follows the link and returns statistics about the link target. Behind the scenes it uses fs.statSync().

getStatisticsAsync(path)

static

An async version of FileSystem.getStatistics().

isDirectoryError(error)

static

Returns true if the error object indicates the target is a directory (EISDIR).

isErrnoException(error)

static

Detects if the provided error object is a NodeJS.ErrnoException

isExistError(error)

static

Returns true if the error object indicates the file or folder already exists (EEXIST).

isFileDoesNotExistError(error)

static

Returns true if the error object indicates the file does not exist (ENOENT).

isFolderDoesNotExistError(error)

static

Returns true if the error object indicates the folder does not exist (ENOTDIR).

isNotDirectoryError(error)

static

Returns true if the error object indicates the target is not a directory (ENOTDIR).

isNotExistError(error)

static

Returns true if the error object indicates the file or folder does not exist (ENOENT or ENOTDIR)

isUnlinkNotPermittedError(error)

static

Returns true if the error object indicates that the unlink system call failed due to a permissions issue (EPERM).

move(options)

static

Moves a file. The folder must exist, unless the ensureFolderExists option is provided. Behind the scenes it uses fs-extra.moveSync()

moveAsync(options)

static

An async version of FileSystem.move().

readFile(filePath, options)

static

Reads the contents of a file into a string. Behind the scenes it uses fs.readFileSync().

readFileAsync(filePath, options)

static

An async version of FileSystem.readFile().

readFileToBuffer(filePath)

static

Reads the contents of a file into a buffer. Behind the scenes is uses fs.readFileSync().

readFileToBufferAsync(filePath)

static

An async version of FileSystem.readFileToBuffer().

readFolderItemNames(folderPath, options)

static

Reads the names of folder entries, not including "." or "..". Behind the scenes it uses fs.readdirSync().

readFolderItemNamesAsync(folderPath, options)

static

An async version of FileSystem.readFolderItemNames().

readFolderItems(folderPath, options)

static

Reads the contents of the folder, not including "." or "..", returning objects including the entry names and types. Behind the scenes it uses fs.readdirSync().

readFolderItemsAsync(folderPath, options)

static

An async version of FileSystem.readFolderItems().

readLink(path)

static

If path refers to a symbolic link, this returns the path of the link target, which may be an absolute or relative path.

readLinkAsync(path)

static

An async version of FileSystem.readLink().

updateTimes(path, times)

static

Updates the accessed and modified timestamps of the filesystem object referenced by path. Behind the scenes it uses fs.utimesSync(). The caller should specify both times in the times parameter.

updateTimesAsync(path, times)

static

An async version of FileSystem.updateTimes().

writeBuffersToFile(filePath, contents, options)

static

Writes the contents of multiple Uint8Arrays to a file on disk, overwriting the file if it already exists. Behind the scenes it uses fs.writevSync().

This API is useful for writing large files efficiently, especially if the input is being concatenated from multiple sources.

writeBuffersToFileAsync(filePath, contents, options)

static

An async version of FileSystem.writeBuffersToFile().

writeFile(filePath, contents, options)

static

Writes a text string to a file on disk, overwriting the file if it already exists. Behind the scenes it uses fs.writeFileSync().

writeFileAsync(filePath, contents, options)

static

An async version of FileSystem.writeFile().