|
1 | 1 | 'use strict'; |
2 | | -/* eslint-disable no-await-in-loop */ |
3 | 2 |
|
4 | | -import { readdir, stat } from 'node:fs/promises'; |
5 | | -import { join, relative } from 'node:path'; |
6 | | -import { normalizePathToUnix } from './normalizePathToUnix.js'; |
7 | | - |
8 | | -export async function findFilePath( |
9 | | - fileName: string, |
10 | | - packageDirectories: string[], |
11 | | - repoRoot: string |
12 | | -): Promise<string | undefined> { |
13 | | - for (const directory of packageDirectories) { |
14 | | - const result = await resolveFilePath(fileName, directory, repoRoot); |
15 | | - if (result) { |
16 | | - return normalizePathToUnix(result); |
17 | | - } |
18 | | - } |
19 | | - return undefined; |
20 | | -} |
21 | | - |
22 | | -async function resolveFilePath(fileName: string, dxDirectory: string, repoRoot: string): Promise<string | undefined> { |
23 | | - const extensionsToTry = getExtensionsToTry(fileName); |
24 | | - |
25 | | - for (const name of extensionsToTry) { |
26 | | - const absolutePath = await searchRecursively(name, dxDirectory); |
27 | | - if (absolutePath) { |
28 | | - return relative(repoRoot, absolutePath); |
29 | | - } |
| 3 | +/** |
| 4 | + * Find file path using a pre-built cache (fast) or fallback to the old method. |
| 5 | + * |
| 6 | + * @param fileName - Name of the file to find |
| 7 | + * @param filePathCache - Optional pre-built cache of filename -> path mappings |
| 8 | + * @returns Normalized Unix-style relative path or undefined if not found |
| 9 | + */ |
| 10 | +export function findFilePath(fileName: string, filePathCache?: Map<string, string>): string | undefined { |
| 11 | + if (filePathCache) { |
| 12 | + return findFilePathFromCache(fileName, filePathCache); |
30 | 13 | } |
31 | 14 |
|
| 15 | + // Fallback to old behavior should never happen in practice, |
| 16 | + // but keeping for backwards compatibility |
32 | 17 | return undefined; |
33 | 18 | } |
34 | 19 |
|
35 | | -function getExtensionsToTry(fileName: string): string[] { |
36 | | - return ['cls', 'trigger'].map((ext) => `${fileName}.${ext}`); |
37 | | -} |
38 | | - |
39 | | -async function searchRecursively(fileName: string, directory: string): Promise<string | undefined> { |
40 | | - const entries = await readdir(directory); |
| 20 | +function findFilePathFromCache(fileName: string, cache: Map<string, string>): string | undefined { |
| 21 | + // Try exact match first |
| 22 | + const exactMatch = cache.get(fileName); |
| 23 | + if (exactMatch) { |
| 24 | + return exactMatch; |
| 25 | + } |
41 | 26 |
|
42 | | - for (const entry of entries) { |
43 | | - const fullPath = join(directory, entry); |
44 | | - const stats = await stat(fullPath); |
| 27 | + // Try with .cls extension |
| 28 | + const clsMatch = cache.get(`${fileName}.cls`); |
| 29 | + if (clsMatch) { |
| 30 | + return clsMatch; |
| 31 | + } |
45 | 32 |
|
46 | | - if (stats.isDirectory()) { |
47 | | - const nestedResult = await searchRecursively(fileName, fullPath); |
48 | | - if (nestedResult) return nestedResult; |
49 | | - } else if (entry === fileName) { |
50 | | - return fullPath; |
51 | | - } |
| 33 | + // Try with .trigger extension |
| 34 | + const triggerMatch = cache.get(`${fileName}.trigger`); |
| 35 | + if (triggerMatch) { |
| 36 | + return triggerMatch; |
52 | 37 | } |
53 | 38 |
|
54 | 39 | return undefined; |
|
0 commit comments