Skip to main content

tools-filesystem

Build npm version

@rnx-kit/tools-filesystem is a collection of commonly used functions for manipulating the filesystem.

You can import the entire package, or, to save space, import individual categories:

import * as tools from "@rnx-kit/tools-filesystem";

// Alternatively...
import * as mocks from "@rnx-kit/tools-filesystem/mocks";

Filesystem helpers

The bulk of this package contains a set of convenience wrappers for working with the filesystem. This ensures people use the same constants when referencing files, handle certain patterns like BOM stripping and new line appending for JSON files correctly, and provides implementations for some more complex patterns like file matching.

MockFS submodule

This package has an export entry for mocking the filesystem for testing. This uses memfs for its implementation. This is marked as an optional peer dependency but it is required when using the mockFS convenience wrapper.

CategoryFunctionDescription
comparefilesMatch(pathA, pathB)Try to efficiently determine if two files match by size and content
comparefilesMatchSync(path1, path2)Compare two files to determine if two files are identical in content. This means either the content matches or they are actually referencing the same file on disk (e.g. via a hardlink).
compareisSameFileFromStats(stats1, stats2)Compares the stats of two files to determine if they refer to the same file. This preempts more expensive file comparisons by comparing the stats first. This will return: - true if the stats indicate the files are the same (e.g. same inode and device) - false if the stats indicate the files are different (e.g. different sizes) - undefined if the stats are inconclusive and further checks are needed (e.g. same size but different inodes)
dirsensureDir(path)Asynchronously ensures that a directory exists, creating it if necessary.
dirsensureDirForFile(p)Asynchronously ensures that the directory for a given file path exists, creating it if necessary.
dirsensureDirForFileSync(p)Ensures that the directory for a given file path exists, creating it if necessary.
dirsensureDirSync(path)Ensures that a directory exists, creating it if necessary.
entrytoFSEntry(path)Helper function to convert a string path to an FSEntry if it is not already an FSEntry
fileioreadJSONFile(filePath)Asynchronously reads the content of a file as JSON, stripping a UTF-8 BOM if present. This is a convenience method that combines readTextFile with JSON.parse, and is useful for reading JSON files without having to worry about BOMs causing parse failures.
fileioreadJSONFileSync(filePath)Synchronously reads the content of a file as JSON, stripping a UTF-8 BOM if present. This is a convenience method that combines readFileSync with JSON.parse, and is useful for reading JSON files without having to worry about BOMs causing parse failures.
fileioreadTextFile(filePath)Asynchronously reads the content of a file as a UTF-8 string.
fileioreadTextFileSync(filePath)Synchronously reads the content of a file as a UTF-8 string.
fileiowriteJSONFile(path, data, space)Writes specified data to a file asynchronously, serialized as JSON format.
fileiowriteJSONFileSync(path, data, space)Writes specified data to a file, serialized as JSON format.
fileiowriteTextFile(path, data)Writes specified data to a file asynchronously, assuming the data is UTF-8 encoded text.
fileiowriteTextFileSync(path, data)Writes specified data to a file, assuming the data is UTF-8 encoded text.
jsonparseJSON(content)Parse JSON via the internal JSON.parse, checking for and stripping a UTF-8 byte order mark if present to avoid parse failures.
jsonserializeJSON(data, space)Serialize data to JSON format, optionally pretty-printing with a specified number of spaces.
jsonstripBOM(content)Strip a UTF-8 BOM (Byte Order Mark) from the beginning of a string, if present.