diff options
| author | 2023-04-28 01:36:44 +0800 | |
|---|---|---|
| committer | 2023-04-28 01:36:44 +0800 | |
| commit | dd84b9d64fb98746a230cd24233ff50a562c39c9 (patch) | |
| tree | b583261ef00b3afe72ec4d6dacb31e57779a6faf /packages/turbo-utils/src/searchUp.ts | |
| parent | 0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff) | |
| download | HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip | |
Diffstat (limited to 'packages/turbo-utils/src/searchUp.ts')
| -rw-r--r-- | packages/turbo-utils/src/searchUp.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/packages/turbo-utils/src/searchUp.ts b/packages/turbo-utils/src/searchUp.ts new file mode 100644 index 0000000..57f92e4 --- /dev/null +++ b/packages/turbo-utils/src/searchUp.ts @@ -0,0 +1,44 @@ +import fs from "fs"; +import path from "path"; + +function searchUp({ + target, + cwd, + contentCheck, +}: { + target: string; + cwd: string; + contentCheck?: (content: string) => boolean; +}): string | null { + const root = path.parse(cwd).root; + + let found = false; + while (!found && cwd !== root) { + if (contentCheck) { + try { + const content = fs.readFileSync(path.join(cwd, target)).toString(); + if (contentCheck(content)) { + found = true; + break; + } + } catch { + // keep looking + } + } else { + if (fs.existsSync(path.join(cwd, target))) { + found = true; + break; + } + } + + cwd = path.dirname(cwd); + } + + if (found) { + return cwd; + } + + return null; +} + +export default searchUp; |
