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/getTurboRoot.ts | |
| parent | 0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff) | |
| download | HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip | |
Diffstat (limited to 'packages/turbo-utils/src/getTurboRoot.ts')
| -rw-r--r-- | packages/turbo-utils/src/getTurboRoot.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/turbo-utils/src/getTurboRoot.ts b/packages/turbo-utils/src/getTurboRoot.ts new file mode 100644 index 0000000..64a37be --- /dev/null +++ b/packages/turbo-utils/src/getTurboRoot.ts @@ -0,0 +1,49 @@ +import { findRootSync } from "@manypkg/find-root"; +import searchUp from "./searchUp"; +import JSON5 from "json5"; + +interface Options { + cache?: boolean; +} + +function contentCheck(content: string): boolean { + const result = JSON5.parse(content); + return !result.extends; +} + +const configCache: Record<string, string> = {}; + +function getTurboRoot(cwd?: string, opts?: Options): string | null { + const cacheEnabled = opts?.cache ?? true; + const currentDir = cwd || process.cwd(); + + if (cacheEnabled && configCache[currentDir]) { + return configCache[currentDir]; + } + + // Turborepo root can be determined by a turbo.json without an extends key + let root = searchUp({ + target: "turbo.json", + cwd: currentDir, + contentCheck, + }); + + if (!root) { + try { + root = findRootSync(currentDir); + if (!root) { + return null; + } + } catch (err) { + return null; + } + } + + if (cacheEnabled) { + configCache[currentDir] = root; + } + + return root; +} + +export default getTurboRoot; |
