default

Streaming file-system utilities for the anabranch ecosystem.

Examples

Read all lines from a file

import { readLines } from "@anabranch/fs";

const { successes } = await readLines("./data.txt").partition();
console.log(successes.join("\n"));

Walk a directory and find TypeScript files

import { glob } from "@anabranch/fs";

const results = await glob("./src", "*.ts").collect();
console.log(results.map((e) => e.path));

Classes

Functions

f
ensureDir(path: string | URL): Task<void, EnsureDirError>

Creates a directory and any missing parents, like mkdir -p.

f
glob(
root: string | URL,
pattern: string,
options?: GlobOptions
): Source<WalkEntry, DirError>

Finds all entries under root whose relative path matches the glob pattern.

f
readDir(path: string | URL): Source<DirEntry, DirError>

Lists the immediate children of a directory.

f
readLines(path: string | URL): Source<string, ReadFileError>

Streams lines from a text file one at a time using node:readline.

f
remove(path: string | URL): Task<void, RemoveError>

Removes a file or directory recursively. No error if the path doesn't exist.

f
stat(path: string | URL): Task<StatInfo, StatError>

Returns metadata for a path without reading its contents.

f
walk(
root: string | URL,
options?: WalkOptions
): Source<WalkEntry, DirError>

Recursively walks a directory tree, yielding each entry.

f
watch(
path: string | URL,
options?: WatchOptions
): Stream<FsEvent, WatchError>

Watches path for file-system changes, yielding a FsEvent for each.

f
writeFile(
path: string | URL,
data: Uint8Array
): Task<void, WriteFileError>

Writes a Uint8Array to a file, creating or overwriting it.

f
writeJson(
path: string | URL,
value: unknown
): Task<void, WriteFileError>

Serialises value as JSON and writes it to a file, creating or overwriting it.

f
writeTextFile(
path: string | URL,
content: string
): Task<void, WriteFileError>

Writes a UTF-8 string to a file, creating or overwriting it.

Interfaces

I
DirEntry

An entry in a directory listing.

I
FsEvent

A file-system change event, as yielded by watch.

I
GlobOptions

Options for glob. Same as WalkOptions without match.

I
StatInfo

File metadata returned by stat.

I
WalkEntry

A directory entry with its full path, as yielded by walk and glob.

I
WalkOptions

Options for walk.

I
WatchOptions

Options for watch.

Type Aliases

T
CopyFileError = NotFound | PermissionDenied | Unknown

Errors that can occur when copying a file.

T
DirError = NotFound | NotDirectory | PermissionDenied | Unknown

Errors that can occur when reading directories.

T
EnsureDirError = PermissionDenied | Unknown

Errors that can occur when ensuring a directory.

T
ExistsError = PermissionDenied | Unknown

Errors that can occur when checking existence.

T
RemoveError = PermissionDenied | Unknown

Errors that can occur when removing a path.

T
StatError = NotFound | PermissionDenied | Unknown

Errors that can occur when stat-ing a path.

T
WatchError = NotFound | PermissionDenied

Errors that can occur when watching the file system.