UNPKG

@stryke/fs

Version:

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

1 lines 3.46 kB
{"version":3,"file":"get-parent-path.mjs","names":["currentDir","cwd"],"sources":["../src/get-parent-path.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { toArray } from \"@stryke/convert/to-array\";\nimport { cwd as currentDir } from \"@stryke/path/cwd\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { resolveParentPath } from \"@stryke/path/resolve-parent-path\";\nimport { existsSync } from \"node:fs\";\nimport { isDirectory } from \"./is-file\";\n\nexport interface GetParentPathOptions {\n /**\n * Whether to ignore the case of the file names when checking for existence.\n *\n * @defaultValue true\n */\n ignoreCase: boolean;\n\n /**\n * Whether to skip the current working directory when checking for the file.\n *\n * @defaultValue false\n */\n skipCwd: boolean;\n\n /**\n * Should we include the found file/directory name in the results.\n *\n * @defaultValue false\n */\n includeNameInResults?: boolean;\n}\n\n/**\n * Get the first parent path that has a file or directory with the provided name.\n *\n * @param name - The name (or names) of the file to look for in the parent paths.\n * @param cwd - The current working directory.\n * @returns The first parent path that exists.\n */\nexport const getParentPath = (\n name: string | string[],\n cwd = currentDir(),\n options: Partial<GetParentPathOptions> = {}\n): string | undefined => {\n const ignoreCase = options?.ignoreCase ?? true;\n const skipCwd = options?.skipCwd ?? false;\n const includeNameInResults = options?.includeNameInResults ?? false;\n\n let dir = cwd;\n if (skipCwd) {\n dir = resolveParentPath(cwd);\n }\n\n let names = toArray(name);\n if (ignoreCase) {\n names = names.map(name => name.toLowerCase());\n }\n\n while (true) {\n const target = names.find(name => existsSync(joinPaths(dir, name)));\n if (target) {\n return includeNameInResults || isDirectory(joinPaths(dir, target))\n ? joinPaths(dir, target)\n : dir;\n }\n\n const parentDir = resolveParentPath(dir);\n if (parentDir === dir) {\n // It'll fail anyway\n return undefined;\n }\n\n dir = parentDir;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;AAuDA,MAAa,iBACX,MACA,QAAMA,KAAY,EAClB,UAAyC,EAAE,KACpB;CACvB,MAAM,aAAa,SAAS,cAAc;CAC1C,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,uBAAuB,SAAS,wBAAwB;CAE9D,IAAI,MAAMC;AACV,KAAI,QACF,OAAM,kBAAkBA,MAAI;CAG9B,IAAI,QAAQ,QAAQ,KAAK;AACzB,KAAI,WACF,SAAQ,MAAM,KAAI,SAAQ,KAAK,aAAa,CAAC;AAG/C,QAAO,MAAM;EACX,MAAM,SAAS,MAAM,MAAK,SAAQ,WAAW,UAAU,KAAK,KAAK,CAAC,CAAC;AACnE,MAAI,OACF,QAAO,wBAAwB,YAAY,UAAU,KAAK,OAAO,CAAC,GAC9D,UAAU,KAAK,OAAO,GACtB;EAGN,MAAM,YAAY,kBAAkB,IAAI;AACxC,MAAI,cAAc,IAEhB;AAGF,QAAM"}