UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

35 lines (33 loc) 1.37 kB
import { isDirectory } from "./is-file.mjs"; import { existsSync } from "node:fs"; import { joinPaths } from "@stryke/path/join-paths"; import { resolveParentPath } from "@stryke/path/resolve-parent-path"; import { toArray } from "@stryke/convert/to-array"; import { cwd } from "@stryke/path/cwd"; //#region src/get-parent-path.ts /** * Get the first parent path that has a file or directory with the provided name. * * @param name - The name (or names) of the file to look for in the parent paths. * @param cwd - The current working directory. * @returns The first parent path that exists. */ const getParentPath = (name, cwd$1 = cwd(), options = {}) => { const ignoreCase = options?.ignoreCase ?? true; const skipCwd = options?.skipCwd ?? false; const includeNameInResults = options?.includeNameInResults ?? false; let dir = cwd$1; if (skipCwd) dir = resolveParentPath(cwd$1); let names = toArray(name); if (ignoreCase) names = names.map((name) => name.toLowerCase()); while (true) { const target = names.find((name) => existsSync(joinPaths(dir, name))); if (target) return includeNameInResults || isDirectory(joinPaths(dir, target)) ? joinPaths(dir, target) : dir; const parentDir = resolveParentPath(dir); if (parentDir === dir) return; dir = parentDir; } }; //#endregion export { getParentPath }; //# sourceMappingURL=get-parent-path.mjs.map