From dd84b9d64fb98746a230cd24233ff50a562c39c9 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 28 Apr 2023 01:36:44 +0800 Subject: --- packages/turbo-utils/src/getTurboRoot.ts | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/turbo-utils/src/getTurboRoot.ts (limited to 'packages/turbo-utils/src/getTurboRoot.ts') 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 = {}; + +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; -- cgit v1.2.3-70-g09d2