Skip to content

Commit

Permalink
Merge pull request #93 from mcarvin8/refactor
Browse files Browse the repository at this point in the history
refactor: remove await in loop
  • Loading branch information
mcarvin8 authored Jan 17, 2025
2 parents 213c440 + 657951a commit 495cc4a
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions src/helpers/getRepoRoot.ts
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);
}

0 comments on commit 495cc4a

Please sign in to comment.