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, mode) 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. 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 this requires elevated permissionsBits). 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 this requires elevated permissionsBits). Behind the scenes it uses fs.symlinkSync().
createSymbolicLinkFolderAsync(options) static An async version of FileSystem.createSymbolicLinkFolder().
createSymbolicLinkJunction(options) static Creates a Windows “directory junction”. Behaves like createSymbolicLinkToFile() on other platforms. 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().
isErrnoException(error) static Detects if the provided error object is a NodeJS.ErrnoException
isFileDoesNotExistError(error) static Returns true if the error provided indicates the file does not exist.
isFolderDoesNotExistError(error) static Returns true if the error provided indicates the folder does not exist.
isNotExistError(error) static Returns true if the error provided indicates the file or folder does not exist.
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().
readFolder(folderPath, options) static Reads the contents of the folder, not including “.” or “..”. Behind the scenes it uses fs.readdirSync().
readFolderAsync(folderPath, options) static An async version of FileSystem.readFolder().
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().
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().