diff --git a/src/parse-source-code/index.ts b/src/parse-source-code/index.ts index 811d11e..5cfe466 100644 --- a/src/parse-source-code/index.ts +++ b/src/parse-source-code/index.ts @@ -107,33 +107,33 @@ export async function listFiles(dirPath: string, recursive: boolean): Promise { + let currentLevel = 0 + while (results.length < LIST_FILES_LIMIT) { + const pattern = currentLevel === 0 ? "*" : `${"*/".repeat(currentLevel)}*` + const filesAtLevel = await globby(pattern, options) + if (filesAtLevel.length === 0) { + break + } + results.push(...filesAtLevel) + if (results.length >= LIST_FILES_LIMIT) { + results = results.slice(0, LIST_FILES_LIMIT) + break + } + currentLevel++ } - - // Add the files found at this level to the result - results.push(...filesAtLevel) - - // If we have reached the max limit, slice the array to the limit and break - if (results.length >= LIST_FILES_LIMIT) { - results = results.slice(0, LIST_FILES_LIMIT) - break - } - - // Move to the next level - currentLevel++ + return results + } + // Timeout after 10 seconds and return partial results + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => reject(new Error("Globbing timeout")), 10_000) + }) + try { + return await Promise.race([globbingProcess(), timeoutPromise]) + } catch (error) { + console.warn("Globbing timed out, returning partial results") + return results } - - return results } function separateFiles(allFiles: string[]): { filesToParse: string[]; remainingFiles: string[] } {