Skip to content

Instantly share code, notes, and snippets.

@cbdyzj
Last active February 28, 2022 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbdyzj/82d2885835fecca55c237007da38386e to your computer and use it in GitHub Desktop.
Save cbdyzj/82d2885835fecca55c237007da38386e to your computer and use it in GitHub Desktop.
Backup GitHub Public Repositories
async function getRepositoryCloneListByPage(name, page = 1) {
const response = await fetch(`https://api.github.com/users/${name}/repos?page=${page}`)
const result = await response.json()
return result.map((it) => {
return it.clone_url
})
}
async function getRepositoryCloneList(name) {
let page = 1
let cloneList = []
let currentCloneList
do {
currentCloneList = await getRepositoryCloneListByPage(name, page)
cloneList.push(...currentCloneList)
page++
} while (currentCloneList.length);
return cloneList
}
async function cloneRepositoryList(name) {
const cloneList = await getRepositoryCloneList(name)
if (!cloneList.length) {
return
}
for (const cloneUrl of cloneList) {
let done = false
while(!done) {
try {
await $`git clone ${cloneUrl}`
done = true
} catch(err) {
console.log('Error:', err.message, ', Try again...')
}
}
}
}
await cloneRepositoryList('cbdyzj')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment