Add 10s timeout to recursive list files

This commit is contained in:
Saoud Rizwan
2024-08-31 00:11:18 -04:00
parent 172aaa524b
commit adb000aed0

View File

@@ -107,33 +107,33 @@ export async function listFiles(dirPath: string, recursive: boolean): Promise<st
// globby doesnt natively support top down level by level globbing, so we implement it ourselves // globby doesnt natively support top down level by level globbing, so we implement it ourselves
async function globbyLevelByLevel(options?: Options) { async function globbyLevelByLevel(options?: Options) {
let results: string[] = [] let results: string[] = []
const globbingProcess = async () => {
let currentLevel = 0 let currentLevel = 0
while (results.length < LIST_FILES_LIMIT) { while (results.length < LIST_FILES_LIMIT) {
// Construct the glob pattern for the current level
const pattern = currentLevel === 0 ? "*" : `${"*/".repeat(currentLevel)}*` const pattern = currentLevel === 0 ? "*" : `${"*/".repeat(currentLevel)}*`
// Get files and directories at the current level
const filesAtLevel = await globby(pattern, options) const filesAtLevel = await globby(pattern, options)
// If no more files found at this level, break the loop
if (filesAtLevel.length === 0) { if (filesAtLevel.length === 0) {
break break
} }
// Add the files found at this level to the result
results.push(...filesAtLevel) results.push(...filesAtLevel)
// If we have reached the max limit, slice the array to the limit and break
if (results.length >= LIST_FILES_LIMIT) { if (results.length >= LIST_FILES_LIMIT) {
results = results.slice(0, LIST_FILES_LIMIT) results = results.slice(0, LIST_FILES_LIMIT)
break break
} }
// Move to the next level
currentLevel++ currentLevel++
} }
return results return results
}
// Timeout after 10 seconds and return partial results
const timeoutPromise = new Promise<string[]>((_, 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
}
} }
function separateFiles(allFiles: string[]): { filesToParse: string[]; remainingFiles: string[] } { function separateFiles(allFiles: string[]): { filesToParse: string[]; remainingFiles: string[] } {