| 74 | } |
| 75 | |
| 76 | export async function fetchGithubRepoFiles(url: string, onProgress?: (msg: string) => void) { |
| 77 | const match = url.match(/github\.com\/([^/]+)\/([^/]+)/); |
| 78 | if (!match) throw new Error("Invalid GitHub URL"); |
| 79 | const [_, owner, repoName] = match; |
| 80 | let treeUrl = `https://api.github.com/repos/${owner}/${repoName}/git/trees/main?recursive=1`; |
| 81 | let res = await fetch(treeUrl); |
| 82 | if (!res.ok) { |
| 83 | treeUrl = `https://api.github.com/repos/${owner}/${repoName}/git/trees/master?recursive=1`; |
| 84 | res = await fetch(treeUrl); |
| 85 | } |
| 86 | if (!res.ok) throw new Error("Could not fetch repo."); |
| 87 | const data = await res.json(); |
| 88 | const filePaths = data.tree |
| 89 | .filter((t: any) => t.type === "blob") |
| 90 | .map((t: any) => t.path) |
| 91 | .filter((p: string) => p.match(/\.(js|ts|jsx|tsx|py|c|h|cpp|hpp|cc|cs|go|rs|rb|php|swift|kt|kts|dart)$/) && !p.includes("node_modules") && !p.includes(".git")); |
| 92 | const files: { path: string, content: string }[] = []; |
| 93 | for (let i = 0; i < Math.min(filePaths.length, 150); i += 10) { |
| 94 | const batch = filePaths.slice(i, i + 10); |
| 95 | await Promise.all(batch.map(async (path: string) => { |
| 96 | try { |
| 97 | let r = await fetch(`https://raw.githubusercontent.com/${owner}/${repoName}/main/${path}`); |
| 98 | if (!r.ok) r = await fetch(`https://raw.githubusercontent.com/${owner}/${repoName}/master/${path}`); |
| 99 | if (r.ok) files.push({ path, content: await r.text() }); |
| 100 | } catch (err) { } |
| 101 | })); |
| 102 | } |
| 103 | return files; |
| 104 | } |