Skip to content

findUpward

findUpward<T>(startDir, check, stop?): Promise<T | undefined>

Defined in: find-upward.ts:30

Walks upward from startDir — startDir itself first, then its parent, then its parent’s parent, and so on — calling check at each directory and returning the first result it produces that isn’t undefined.

stop, when given, is checked at a directory only after check has already come back empty there (so that directory is still fully checked before stopping) — return true to end the walk there without a result, instead of continuing to its parent. Common use: stop at a repo root (see hasGitEntry) rather than wandering into an unrelated enclosing directory tree.

Always terminates: directory nesting is finite, and walking stops unconditionally once the filesystem root is reached (its own parent).

T

string

(dir) => T | Promise<T | undefined> | undefined

(dir) => boolean | Promise<boolean>

Promise<T | undefined>

import { findUpward, hasGitEntry } from "@theholocron/fs-utils";
const configDir = await findUpward(
process.cwd(),
async (dir) => ((await fileExists(join(dir, "app.config.json"))) ? dir : undefined),
hasGitEntry
);