(
path: string,
{ repoURL = this.repoURL, branch = this.branch, depth = 1 } = {},
folderSupport?: boolean,
)
| 382 | } |
| 383 | |
| 384 | async listFiles( |
| 385 | path: string, |
| 386 | { repoURL = this.repoURL, branch = this.branch, depth = 1 } = {}, |
| 387 | folderSupport?: boolean, |
| 388 | ): Promise<{ type: string; id: string; name: string; path: string; size: number }[]> { |
| 389 | const folder = trim(path, '/'); |
| 390 | const hasFolder = Boolean(folder); |
| 391 | try { |
| 392 | const branchInfo = (await this.request( |
| 393 | `${repoURL}/branches/${encodeURIComponent(branch)}`, |
| 394 | )) as ForgejoBranch; |
| 395 | const treeSha = branchInfo.commit.id; |
| 396 | const useRecursive = depth > 1 || hasFolder; |
| 397 | const result: GitGetTreeResponse = await this.request( |
| 398 | `${repoURL}/git/trees/${encodeURIComponent(treeSha)}`, |
| 399 | { |
| 400 | // Use recursive tree when we need to filter by folder or deeper depth. |
| 401 | params: useRecursive ? { recursive: 1 } : {}, |
| 402 | }, |
| 403 | ); |
| 404 | return ( |
| 405 | result.tree |
| 406 | // filter only files and/or folders up to the required depth |
| 407 | .filter(file => { |
| 408 | if ((!folderSupport ? file.type === 'blob' : true) && file.path) { |
| 409 | if (!hasFolder) { |
| 410 | return file.path.split('/').length <= depth; |
| 411 | } |
| 412 | |
| 413 | if (!file.path.startsWith(`${folder}/`)) { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | const relativePath = file.path.slice(folder.length + 1); |
| 418 | if (!relativePath) { |
| 419 | return false; |
| 420 | } |
| 421 | return relativePath.split('/').length <= depth; |
| 422 | } |
| 423 | return false; |
| 424 | }) |
| 425 | .map(file => { |
| 426 | const relativePath = |
| 427 | hasFolder && file.path.startsWith(`${folder}/`) |
| 428 | ? file.path.slice(folder.length + 1) |
| 429 | : file.path; |
| 430 | return { |
| 431 | type: file.type, |
| 432 | id: file.sha, |
| 433 | name: basename(file.path), |
| 434 | path: hasFolder ? `${folder}/${relativePath}` : file.path, |
| 435 | size: file.size!, |
| 436 | }; |
| 437 | }) |
| 438 | ); |
| 439 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 440 | } catch (err: any) { |
| 441 | if (err && err.status === 404) { |
no test coverage detected