| 123 | } |
| 124 | |
| 125 | export async function fetchGithubRepoFiles(url: string, onProgress?: (msg: string) => void) { |
| 126 | const match = url.match(/github\.com\/([^/]+)\/([^/]+)/); |
| 127 | if (!match) throw new Error("Invalid GitHub URL"); |
| 128 | const [_, owner, repoName] = match; |
| 129 | let treeUrl = `https://api.github.com/repos/${owner}/${repoName}/git/trees/main?recursive=1`; |
| 130 | let res = await fetch(treeUrl); |
| 131 | if (!res.ok) { |
| 132 | treeUrl = `https://api.github.com/repos/${owner}/${repoName}/git/trees/master?recursive=1`; |
| 133 | res = await fetch(treeUrl); |
| 134 | } |
| 135 | if (!res.ok) throw new Error("Could not fetch repo."); |
| 136 | const data = await res.json(); |
| 137 | const filePaths = data.tree |
| 138 | .filter((t: any) => t.type === "blob") |
| 139 | .map((t: any) => t.path) |
| 140 | .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")); |
| 141 | const files: { path: string, content: string }[] = []; |
| 142 | for (let i = 0; i < Math.min(filePaths.length, 150); i += 10) { |
| 143 | const batch = filePaths.slice(i, i + 10); |
| 144 | await Promise.all(batch.map(async (path: string) => { |
| 145 | try { |
| 146 | let r = await fetch(`https://raw.githubusercontent.com/${owner}/${repoName}/main/${path}`); |
| 147 | if (!r.ok) r = await fetch(`https://raw.githubusercontent.com/${owner}/${repoName}/master/${path}`); |
| 148 | if (r.ok) files.push({ path, content: await r.text() }); |
| 149 | } catch (err) { } |
| 150 | })); |
| 151 | } |
| 152 | return files; |
| 153 | } |