commit
940cad0455
@ -0,0 +1,49 @@ |
||||
// eslint-disable-next-line no-unused-vars
|
||||
import axios, { AxiosResponse } from 'axios' |
||||
|
||||
export type GithubItem = { |
||||
name: string |
||||
path: string |
||||
sha: string |
||||
size: number |
||||
url: string |
||||
html_url: string |
||||
git_url: string |
||||
download_url: string | null |
||||
type: 'dir' | 'file' |
||||
_links: { |
||||
self: string |
||||
git: string |
||||
html: string |
||||
} |
||||
} |
||||
|
||||
export const githubFolderResolver = async (url, obj = {}, maxDepth, depth?, rootPath?) => { |
||||
depth = depth ? depth : 0 |
||||
const child = await pullFolder(url) |
||||
depth = depth++ |
||||
const urlObj = new URL(url); |
||||
const pathname = urlObj.pathname; |
||||
const pathParts = pathname.split('/'); |
||||
const folderPath = pathParts.slice(5).join('/'); |
||||
rootPath = rootPath || folderPath |
||||
|
||||
if (!Array.isArray(child)) return obj |
||||
for (const item of child) { |
||||
if (item.type === 'file') { |
||||
const response: AxiosResponse = await axios.get(item.download_url) |
||||
obj[item.path.replace(rootPath, '')] = response.data |
||||
} else if (maxDepth > depth) { |
||||
// dir
|
||||
await githubFolderResolver(item.html_url, obj, maxDepth, depth, rootPath) |
||||
} |
||||
} |
||||
return obj |
||||
} |
||||
|
||||
const pullFolder = async (url) => { |
||||
const response = await axios.get('https://ghfolderpull.remixproject.org', { params: { ghfolder: url } }); |
||||
const data: Array<GithubItem> = await response.data; |
||||
return data |
||||
} |
||||
|
@ -1 +1,2 @@ |
||||
export { RemixURLResolver } from './resolve' |
||||
export { githubFolderResolver } from './github-folder-resolver' |
||||
|
Loading…
Reference in new issue