{"version":3,"sources":["../../src/utilities/findUp.ts"],"sourcesContent":["import fs from 'fs'\nimport path from 'path'\n\n/**\n * Synchronously walks up parent directories until a condition is met and/or one of the file names within the fileNames array is found.\n */\nexport function findUpSync({\n condition,\n dir,\n fileNames,\n}: {\n condition?: (dir: string) => boolean | Promise | string\n dir: string\n fileNames?: string[]\n}): null | string {\n const { root } = path.parse(dir)\n\n while (true) {\n if (fileNames?.length) {\n let found = false\n for (const fileName of fileNames) {\n const filePath = path.join(dir, fileName)\n const exists = pathExistsAndIsAccessibleSync(filePath)\n if (exists) {\n if (!condition) {\n return filePath\n }\n found = true\n break\n }\n }\n if (!found && dir !== root) {\n dir = path.dirname(dir) // Move up one directory level.\n continue\n }\n }\n const result = condition?.(dir)\n if (result === true) {\n return dir\n }\n if (typeof result === 'string' && result?.length) {\n return result\n }\n if (dir === root) {\n return null // Reached the root directory without a match.\n }\n dir = path.dirname(dir) // Move up one directory level.\n }\n}\n\n/**\n * Asynchronously walks up parent directories until a condition is met and/or one of the file names within the fileNames array is found.\n */\nexport async function findUp({\n condition,\n dir,\n fileNames,\n}: {\n condition?: (dir: string) => boolean | Promise | string\n dir: string\n fileNames?: string[]\n}): Promise {\n const { root } = path.parse(dir)\n\n while (true) {\n if (fileNames?.length) {\n let found = false\n for (const fileName of fileNames) {\n const filePath = path.resolve(dir, fileName)\n const exists = await pathExistsAndIsAccessible(filePath)\n if (exists) {\n if (!condition) {\n return filePath\n }\n found = true\n break\n }\n }\n if (!found && dir !== root) {\n dir = path.dirname(dir) // Move up one directory level.\n continue\n }\n }\n const result = await condition?.(dir)\n if (result === true) {\n return dir\n }\n if (typeof result === 'string' && result?.length) {\n return result\n }\n if (dir === root) {\n return null // Reached the root directory without a match.\n }\n dir = path.dirname(dir) // Move up one directory level.\n }\n}\n\n// From https://github.com/sindresorhus/path-exists/blob/main/index.js\n// fs.accessSync is preferred over fs.existsSync as it's usually a good idea\n// to check if the process has permission to read/write to a file before doing so.\n// Also see https://github.com/nodejs/node/issues/39960\nexport function pathExistsAndIsAccessibleSync(path: string) {\n try {\n fs.accessSync(path)\n return true\n } catch {\n return false\n }\n}\n\nexport async function pathExistsAndIsAccessible(path: string) {\n try {\n await fs.promises.access(path)\n return true\n } catch {\n return false\n }\n}\n"],"names":["fs","path","findUpSync","condition","dir","fileNames","root","parse","length","found","fileName","filePath","join","exists","pathExistsAndIsAccessibleSync","dirname","result","findUp","resolve","pathExistsAndIsAccessible","accessSync","promises","access"],"mappings":"AAAA,OAAOA,QAAQ,KAAI;AACnB,OAAOC,UAAU,OAAM;AAEvB;;CAEC,GACD,OAAO,SAASC,WAAW,EACzBC,SAAS,EACTC,GAAG,EACHC,SAAS,EAKV;IACC,MAAM,EAAEC,IAAI,EAAE,GAAGL,KAAKM,KAAK,CAACH;IAE5B,MAAO,KAAM;QACX,IAAIC,WAAWG,QAAQ;YACrB,IAAIC,QAAQ;YACZ,KAAK,MAAMC,YAAYL,UAAW;gBAChC,MAAMM,WAAWV,KAAKW,IAAI,CAACR,KAAKM;gBAChC,MAAMG,SAASC,8BAA8BH;gBAC7C,IAAIE,QAAQ;oBACV,IAAI,CAACV,WAAW;wBACd,OAAOQ;oBACT;oBACAF,QAAQ;oBACR;gBACF;YACF;YACA,IAAI,CAACA,SAASL,QAAQE,MAAM;gBAC1BF,MAAMH,KAAKc,OAAO,CAACX,MAAK,+BAA+B;gBACvD;YACF;QACF;QACA,MAAMY,SAASb,YAAYC;QAC3B,IAAIY,WAAW,MAAM;YACnB,OAAOZ;QACT;QACA,IAAI,OAAOY,WAAW,YAAYA,QAAQR,QAAQ;YAChD,OAAOQ;QACT;QACA,IAAIZ,QAAQE,MAAM;YAChB,OAAO,KAAK,8CAA8C;;QAC5D;QACAF,MAAMH,KAAKc,OAAO,CAACX,MAAK,+BAA+B;IACzD;AACF;AAEA;;CAEC,GACD,OAAO,eAAea,OAAO,EAC3Bd,SAAS,EACTC,GAAG,EACHC,SAAS,EAKV;IACC,MAAM,EAAEC,IAAI,EAA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