( ref: RepoRef, branchesToTry: string[] )
| 254 | |
| 255 | /** List all file paths in a repository (fallback when ZIP download fails). */ |
| 256 | export async function listRepositoryFiles( |
| 257 | ref: RepoRef, |
| 258 | branchesToTry: string[] |
| 259 | ): Promise<{ files: string[]; branch: string; commitSha: string }> { |
| 260 | const headers = getAuthHeaders(ref); |
| 261 | let filesList: string[] = []; |
| 262 | let successBranch = branchesToTry[0] || "main"; |
| 263 | let commitSha = ""; |
| 264 | |
| 265 | if (ref.provider === "github") { |
| 266 | for (const branch of branchesToTry) { |
| 267 | const jsdelivrMetaUrl = getJsdelivrMetaUrl(ref, branch); |
| 268 | if (jsdelivrMetaUrl) { |
| 269 | try { |
| 270 | const metaRes = await fetch(jsdelivrMetaUrl); |
| 271 | if (metaRes.ok) { |
| 272 | const metaData = await metaRes.json(); |
| 273 | if (metaData?.files && Array.isArray(metaData.files)) { |
| 274 | filesList = flattenJsdelivrTree(metaData.files); |
| 275 | successBranch = branch; |
| 276 | if (metaData.version) commitSha = metaData.version; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } catch (e) { |
| 281 | console.warn(`[Explore] jsDelivr listing failed for @${branch}:`, e); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if (filesList.length === 0) { |
| 287 | for (const branch of branchesToTry) { |
| 288 | try { |
| 289 | const treeResponse = await fetch( |
| 290 | `https://api.github.com/repos/${ref.owner}/${ref.repo}/git/trees/${branch}?recursive=true`, |
| 291 | { headers } |
| 292 | ); |
| 293 | if (treeResponse.ok) { |
| 294 | const treeData = await treeResponse.json(); |
| 295 | if (treeData?.tree && Array.isArray(treeData.tree)) { |
| 296 | filesList = treeData.tree |
| 297 | .filter((item: { type: string }) => item.type === "blob") |
| 298 | .map((item: { path: string }) => item.path); |
| 299 | successBranch = branch; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | } catch (e) { |
| 304 | console.warn(`[Explore] GitHub tree listing failed for @${branch}:`, e); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } else { |
| 309 | const projectPath = encodeURIComponent(ref.fullPath); |
| 310 | for (const branch of branchesToTry) { |
| 311 | try { |
| 312 | let page = 1; |
| 313 | const pageFiles: string[] = []; |
no test coverage detected