diff --git a/src/helpers/getRepoRoot.ts b/src/helpers/getRepoRoot.ts index d072eb2..c1d89aa 100644 --- a/src/helpers/getRepoRoot.ts +++ b/src/helpers/getRepoRoot.ts @@ -1,32 +1,25 @@ 'use strict'; -/* eslint-disable no-await-in-loop */ import { access } from 'node:fs/promises'; import { join, dirname } from 'node:path'; -export async function getRepoRoot(): Promise<{ repoRoot: string | undefined; dxConfigFilePath: string | undefined }> { - let currentDir = process.cwd(); - let found = false; - let dxConfigFilePath: string | undefined; - let repoRoot: string | undefined; - - do { - const filePath = join(currentDir, 'sfdx-project.json'); - - try { - // Check if sfdx-project.json exists in the current directory - await access(filePath); - dxConfigFilePath = filePath; - repoRoot = currentDir; - found = true; - } catch { - // If file not found, move up one directory level - const parentDir = dirname(currentDir); - if (currentDir === parentDir) { - // Reached the root without finding the file, throw an error - throw new Error('sfdx-project.json not found in any parent directory.'); - } - currentDir = parentDir; +async function findRepoRoot(dir: string): Promise<{ repoRoot: string | undefined; dxConfigFilePath: string | undefined }> { + const filePath = join(dir, 'sfdx-project.json'); + try { + // Check if sfdx-project.json exists in the current directory + await access(filePath); + return { repoRoot: dir, dxConfigFilePath: filePath }; + } catch { + const parentDir = dirname(dir); + if (dir === parentDir) { + // Reached the root without finding the file, throw an error + throw new Error('sfdx-project.json not found in any parent directory.'); } - } while (!found); - return { repoRoot, dxConfigFilePath }; + // Recursively search in the parent directory + return findRepoRoot(parentDir); + } +} + +export async function getRepoRoot(): Promise<{ repoRoot: string | undefined; dxConfigFilePath: string | undefined }> { + const currentDir = process.cwd(); + return findRepoRoot(currentDir); }