(params: {
client: GitHubClient;
owner: string;
repo: string;
path: string;
ref?: string;
depth?: number;
semaphore: Semaphore;
})
| 22 | * Recursively list all files in a GitHub directory. |
| 23 | */ |
| 24 | export async function listDirectoryRecursive(params: { |
| 25 | client: GitHubClient; |
| 26 | owner: string; |
| 27 | repo: string; |
| 28 | path: string; |
| 29 | ref?: string; |
| 30 | depth?: number; |
| 31 | semaphore: Semaphore; |
| 32 | }): Promise<GitHubFileEntry[]> { |
| 33 | const { client, owner, repo, path, ref, depth = 0, semaphore } = params; |
| 34 | |
| 35 | if (depth > MAX_RECURSION_DEPTH) { |
| 36 | throw new Error( |
| 37 | `Maximum recursion depth (${MAX_RECURSION_DEPTH}) exceeded while listing directory: ${path}`, |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | // Semaphore is released here before recursive Promise.all below to avoid deadlock |
| 42 | const entries = await withSemaphore(semaphore, () => |
| 43 | client.listDirectory(owner, repo, path, ref), |
| 44 | ); |
| 45 | |
| 46 | const files: GitHubFileEntry[] = []; |
| 47 | const directories: GitHubFileEntry[] = []; |
| 48 | |
| 49 | for (const entry of entries) { |
| 50 | if (entry.type === "file") { |
| 51 | files.push(entry); |
| 52 | } else if (entry.type === "dir") { |
| 53 | directories.push(entry); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | const subResults = await Promise.all( |
| 58 | directories.map((dir) => |
| 59 | listDirectoryRecursive({ |
| 60 | client, |
| 61 | owner, |
| 62 | repo, |
| 63 | path: dir.path, |
| 64 | ref, |
| 65 | depth: depth + 1, |
| 66 | semaphore, |
| 67 | }), |
| 68 | ), |
| 69 | ); |
| 70 | |
| 71 | return [...files, ...subResults.flat()]; |
| 72 | } |
no test coverage detected
searching dependent graphs…