UNPKG

@stryke/fs

Version:

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

36 lines (34 loc) 1.77 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); const require_is_file = require('./is-file.cjs'); let node_fs = require("node:fs"); let _stryke_path_join_paths = require("@stryke/path/join-paths"); let _stryke_path_resolve_parent_path = require("@stryke/path/resolve-parent-path"); let _stryke_convert_to_array = require("@stryke/convert/to-array"); let _stryke_path_cwd = require("@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 = (0, _stryke_path_cwd.cwd)(), options = {}) => { const ignoreCase = options?.ignoreCase ?? true; const skipCwd = options?.skipCwd ?? false; const includeNameInResults = options?.includeNameInResults ?? false; let dir = cwd; if (skipCwd) dir = (0, _stryke_path_resolve_parent_path.resolveParentPath)(cwd); let names = (0, _stryke_convert_to_array.toArray)(name); if (ignoreCase) names = names.map((name) => name.toLowerCase()); while (true) { const target = names.find((name) => (0, node_fs.existsSync)((0, _stryke_path_join_paths.joinPaths)(dir, name))); if (target) return includeNameInResults || require_is_file.isDirectory((0, _stryke_path_join_paths.joinPaths)(dir, target)) ? (0, _stryke_path_join_paths.joinPaths)(dir, target) : dir; const parentDir = (0, _stryke_path_resolve_parent_path.resolveParentPath)(dir); if (parentDir === dir) return; dir = parentDir; } }; //#endregion exports.getParentPath = getParentPath;