From adb000aed0164ba1f54438b016fb04660d7bfa4a Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Sat, 31 Aug 2024 00:11:18 -0400 Subject: [PATCH] Add 10s timeout to recursive list files --- src/parse-source-code/index.ts | 50 +++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) 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[] } {