Skip to content

Commit

Permalink
refactor: remove await in loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Jan 18, 2025
1 parent a3356f9 commit 705bfc3
Showing 1 changed file with 33 additions and 40 deletions.
73 changes: 33 additions & 40 deletions src/helpers/findFilePath.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
/* eslint-disable no-await-in-loop */

import { readdir, stat } from 'node:fs/promises';
import { readdir } from 'node:fs/promises';
import { join, relative } from 'node:path';
import { normalizePathToUnix } from './normalizePathToUnix.js';

Expand All @@ -10,56 +9,50 @@ export async function findFilePath(
packageDirectories: string[],
repoRoot: string
): Promise<string | undefined> {
let relativeFilePath: string | undefined;
for (const directory of packageDirectories) {
relativeFilePath = await findFilePathinDirectory(fileName, directory, repoRoot);
if (relativeFilePath !== undefined) {
relativeFilePath = normalizePathToUnix(relativeFilePath)
break;
const tasks = packageDirectories.map((directory) => findFilePathInDirectory(fileName, directory, repoRoot));

for await (const relativeFilePath of tasks) {
if (relativeFilePath) {
return normalizePathToUnix(relativeFilePath);
}
}
return relativeFilePath;
return undefined;
}

async function searchRecursively(fileName: string, dxDirectory: string): Promise<string | undefined> {
const files = await readdir(dxDirectory);
for (const file of files) {
const filePath = join(dxDirectory, file);
const stats = await stat(filePath);
if (stats.isDirectory()) {
const result = await searchRecursively(fileName, filePath);
if (result) {
return result;
}
} else if (file === fileName) {
return filePath;
async function searchRecursively(fileName: string, dir: string): Promise<string | undefined> {
const entries = await readdir(dir, { withFileTypes: true });

const tasks = entries.map((entry) => {
const entryPath = join(dir, entry.name);
if (entry.isDirectory()) {
return searchRecursively(fileName, entryPath);
} else if (entry.isFile() && entry.name === fileName) {
return Promise.resolve(entryPath);
}
}
return undefined;
return Promise.resolve(undefined);
});

const results = await Promise.all(tasks);
return results.find((result) => result !== undefined);
}

async function findFilePathinDirectory(
async function findFilePathInDirectory(
fileName: string,
dxDirectory: string,
directory: string,
repoRoot: string
): Promise<string | undefined> {
const fileExtension = fileName.split('.').slice(1).join('.');
let relativeFilePath: string | undefined;

if (fileExtension) {
// If file extension is defined, search recursively with that extension
const absoluteFilePath = await searchRecursively(fileName, dxDirectory);
if (absoluteFilePath !== undefined) relativeFilePath = relative(repoRoot, absoluteFilePath);
} else {
// If file extension is not defined, test each extension option
const fileExts: string[] = ['cls', 'trigger'];
for (const ext of fileExts) {
const absoluteFilePath = await searchRecursively(`${fileName}.${ext}`, dxDirectory);
if (absoluteFilePath !== undefined) {
relativeFilePath = relative(repoRoot, absoluteFilePath);
break;
}
}
const absoluteFilePath = await searchRecursively(fileName, directory);
return absoluteFilePath ? relative(repoRoot, absoluteFilePath) : undefined;
}
return relativeFilePath;

const fileExtensions = ['cls', 'trigger'];
const tasks = fileExtensions.map((ext) => searchRecursively(`${fileName}.${ext}`, directory));

const results = await Promise.all(tasks);
const absoluteFilePath = results.find((result) => result !== undefined);

return absoluteFilePath ? relative(repoRoot, absoluteFilePath) : undefined;
}

0 comments on commit 705bfc3

Please sign in to comment.