-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from mcarvin8/refactor
refactor: remove await in loop
- Loading branch information
Showing
1 changed file
with
19 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |