Skip to content
Warlock.js v4

API

The complete public surface of @warlock.js/fs, grouped by module. Every export. Same naming convention throughout: *Async returns a Promise, the bare name is synchronous.

getFileAsync(path: string): Promise<string>

Section titled “getFileAsync(path: string): Promise<string>”

Read a file as UTF-8 text. Throws ENOENT if missing.

const config = await getFileAsync("./config.toml");

Sync version of getFileAsync.

getJsonFileAsync<T = unknown>(path: string): Promise<T>

Section titled “getJsonFileAsync<T = unknown>(path: string): Promise<T>”

Read and JSON.parse a file. Throws ENOENT if missing, SyntaxError if invalid JSON.

const manifest = await getJsonFileAsync<Manifest>("./manifest.json");

Sync version of getJsonFileAsync.

putFileAsync(filePath: string, content: string): Promise<void>

Section titled “putFileAsync(filePath: string, content: string): Promise<void>”

Write a UTF-8 string to disk. Creates missing parent directories. Overwrites existing files. Text only — for binary, use atomicWriteAsync or drop to node:fs/promises.

await putFileAsync("./out/log.txt", "hello\n");

putFile(filePath: string, content: string): void

Section titled “putFile(filePath: string, content: string): void”

Sync version of putFileAsync.

putJsonFileAsync(filePath: string, value: unknown): Promise<void>

Section titled “putJsonFileAsync(filePath: string, value: unknown): Promise<void>”

Write a JSON-serializable value as pretty-printed JSON (2-space indent).

await putJsonFileAsync("./out/manifest.json", { version: "1.0.0" });

putJsonFile(filePath: string, value: unknown): void

Section titled “putJsonFile(filePath: string, value: unknown): void”

Sync version of putJsonFileAsync.

atomicWriteAsync(filePath: string, content: string | Buffer): Promise<void>

Section titled “atomicWriteAsync(filePath: string, content: string | Buffer): Promise<void>”

Write a file atomically — writes to a uniquely-named sibling temp file then renames onto the target. Readers see old or new content, never a half-written file. Creates missing parent directories. Accepts string or Buffer. Async only.

await atomicWriteAsync("./config.toml", configString);
await atomicWriteAsync("./binary.bin", Buffer.from([0x01, 0x02]));

atomicWriteJsonAsync(filePath: string, value: unknown): Promise<void>

Section titled “atomicWriteJsonAsync(filePath: string, value: unknown): Promise<void>”

Atomic write convenience for JSON values. Pretty-prints at 2-space indent.

await atomicWriteJsonAsync("./manifest.json", { version: "1.0.0" });

ensureDirectoryAsync(path: string): Promise<void>

Section titled “ensureDirectoryAsync(path: string): Promise<void>”

Create a directory recursively. Idempotent — no error if it already exists.

await ensureDirectoryAsync("./dist/cache/v2");

Sync version.

removeDirectoryAsync(path: string): Promise<void>

Section titled “removeDirectoryAsync(path: string): Promise<void>”

Recursively delete a directory and its contents. No error if missing (uses rm({ recursive: true, force: true })).

await removeDirectoryAsync("./dist");

Sync version.

listAsync(directoryPath: string): Promise<string[]>

Section titled “listAsync(directoryPath: string): Promise<string[]>”

List immediate children (files + subdirectories) as full paths joined to the directory.

const entries = await listAsync("./src");
// → ["./src/a.ts", "./src/components", ...]

Sync version.

listFilesAsync(directoryPath: string): Promise<string[]>

Section titled “listFilesAsync(directoryPath: string): Promise<string[]>”

List only regular files in a directory (not subdirectories). Full paths.

const files = await listFilesAsync("./src");

listFiles(directoryPath: string): string[]

Section titled “listFiles(directoryPath: string): string[]”

Sync version.

listDirectoriesAsync(directoryPath: string): Promise<string[]>

Section titled “listDirectoriesAsync(directoryPath: string): Promise<string[]>”

List only subdirectories. Full paths.

const dirs = await listDirectoriesAsync("./src");

listDirectories(directoryPath: string): string[]

Section titled “listDirectories(directoryPath: string): string[]”

Sync version.

copyFileAsync(source: string, destination: string): Promise<void>

Section titled “copyFileAsync(source: string, destination: string): Promise<void>”

Copy a single file. Creates the destination’s parent directory if missing.

await copyFileAsync("./src.txt", "./dst/copy.txt");

copyFile(source: string, destination: string): void

Section titled “copyFile(source: string, destination: string): void”

Sync version.

copyDirectoryAsync(source: string, destination: string): Promise<void>

Section titled “copyDirectoryAsync(source: string, destination: string): Promise<void>”

Recursively copy a directory. Overwrites existing files at the destination.

await copyDirectoryAsync("./public", "./dist/public");

copyDirectory(source: string, destination: string): void

Section titled “copyDirectory(source: string, destination: string): void”

Sync version.

renameFileAsync(from: string, to: string): Promise<void>

Section titled “renameFileAsync(from: string, to: string): Promise<void>”

Rename / move a file or directory. Does not auto-create the destination’s parent — use ensureDirectoryAsync first if needed. Fails with EXDEV for cross-mount moves.

await renameFileAsync("./tmp/foo.txt", "./final/foo.txt");

renameFile(from: string, to: string): void

Section titled “renameFile(from: string, to: string): void”

Sync version.

Delete a single file. ENOENT-safe (no error if missing).

await unlinkAsync("./obsolete.txt");

Sync version.

(removeDirectoryAsync / removeDirectory cover recursive deletes — see Directories.)

pathExistsAsync(path: string): Promise<boolean>

Section titled “pathExistsAsync(path: string): Promise<boolean>”

True if the path exists, regardless of whether it’s a file or directory.

if (await pathExistsAsync("./anything")) { /* ... */ }

Sync version.

fileExistsAsync(path: string): Promise<boolean>

Section titled “fileExistsAsync(path: string): Promise<boolean>”

True only if the path exists AND is a regular file. Follows symlinks (uses stat, not lstat).

if (await fileExistsAsync("./config.toml")) { /* ... */ }

Sync version.

directoryExistsAsync(path: string): Promise<boolean>

Section titled “directoryExistsAsync(path: string): Promise<boolean>”

True only if the path exists AND is a directory.

if (await directoryExistsAsync("./dist")) { /* ... */ }

Sync version.

lastModifiedAsync(path: string): Promise<Date>

Section titled “lastModifiedAsync(path: string): Promise<Date>”

The path’s mtime as a Date. Throws ENOENT if missing.

const mtime = await lastModifiedAsync("./bundle.js");

Sync version.

statsAsync(path: string): Promise<fs.Stats>

Section titled “statsAsync(path: string): Promise<fs.Stats>”

Raw fs.Stats for the path — size, mode, atime, etc.

const stats = await statsAsync("./bundle.js");
console.log(stats.size, stats.mode);

Sync version.

type HashAlgorithm = "sha256" | "sha1" | "md5" | "sha512"

Section titled “type HashAlgorithm = "sha256" | "sha1" | "md5" | "sha512"”

The four supported hash algorithms. Default for every hash function is "sha256".

hashFileAsync(path: string, algorithm?: HashAlgorithm): Promise<string>

Section titled “hashFileAsync(path: string, algorithm?: HashAlgorithm): Promise<string>”

Streaming hex digest of a file’s contents. Constant memory regardless of file size — safe for large files.

const fingerprint = await hashFileAsync("./bundle.js");
const md5 = await hashFileAsync("./bundle.js", "md5");

hashFile(path: string, algorithm?: HashAlgorithm): string

Section titled “hashFile(path: string, algorithm?: HashAlgorithm): string”

Sync — reads the whole file into memory before hashing. Use only on small files.

hashFileSmallAsync(path: string, algorithm?: HashAlgorithm): Promise<string>

Section titled “hashFileSmallAsync(path: string, algorithm?: HashAlgorithm): Promise<string>”

Async, one-shot — reads the file in a single readFile then hashes. Slightly faster than hashFileAsync for files under ~1 MB. Don’t use on large files (loads the whole thing into memory).

const digest = await hashFileSmallAsync("./icon.svg");

hashString(content: string, algorithm?: HashAlgorithm): string

Section titled “hashString(content: string, algorithm?: HashAlgorithm): string”

Hex digest of an in-memory string.

const key = hashString(JSON.stringify(input));

hashBuffer(content: Buffer | Uint8Array, algorithm?: HashAlgorithm): string

Section titled “hashBuffer(content: Buffer | Uint8Array, algorithm?: HashAlgorithm): string”

Hex digest of an in-memory Buffer or Uint8Array.

const digest = hashBuffer(Buffer.from([0x01, 0x02, 0x03]));

Source: @warlock.js/fs/src/