Created
April 14, 2025 01:37
-
-
Save deadflowers/ded6cfcc884d219261a95db16c0fe449 to your computer and use it in GitHub Desktop.
gitclone node version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fetch = require('node-fetch'); | |
const prompt = require('prompt-sync')({ sigint: true }); | |
const { exec } = require('child_process'); | |
const fs = require('fs'); | |
const path = require('path'); | |
// Color utilities | |
const echo_r = (text) => console.log('\x1b[31m%s\x1b[0m', text); | |
const echo_g = (text) => console.log('\x1b[32m%s\x1b[0m', text); | |
const echo_y = (text) => console.log('\x1b[33m%s\x1b[0m', text); | |
const GITCMD = 'git clone'; | |
(async () => { | |
echo_r('Enter the GitHub user or org name:'); | |
const GITORG = prompt('> ').trim(); | |
const REPO_URLS = []; | |
let page = 1; | |
const per_page = 100; | |
echo_y(`Fetching repos for '${GITORG}'...`); | |
while (true) { | |
const res = await fetch(`https://api.github.com/users/${GITORG}/repos?per_page=${per_page}&page=${page}`); | |
if (!res.ok) { | |
echo_r(`Error fetching page ${page}: ${res.statusText}`); | |
process.exit(1); | |
} | |
const repos = await res.json(); | |
if (repos.length === 0) break; | |
repos.forEach(repo => REPO_URLS.push(repo.clone_url)); | |
page++; | |
} | |
if (REPO_URLS.length === 0) { | |
echo_r(`No repositories found for '${GITORG}'`); | |
process.exit(0); | |
} | |
echo_y(`\nFound ${REPO_URLS.length} repositories:`); | |
REPO_URLS.forEach(url => console.log(url)); | |
echo_r('\nPress Enter to proceed with cloning or Ctrl+C to cancel...'); | |
prompt(); | |
const targetDir = path.join(process.cwd(), GITORG); | |
if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir); | |
process.chdir(targetDir); | |
for (const repo of REPO_URLS) { | |
echo_g(`Cloning ${repo}...`); | |
try { | |
await new Promise((resolve, reject) => { | |
exec(`${GITCMD} ${repo}`, (error, stdout, stderr) => { | |
if (error) { | |
echo_r(`Error cloning ${repo}: ${stderr}`); | |
resolve(); // continue with next repo | |
} else { | |
resolve(); | |
} | |
}); | |
}); | |
} catch (e) { | |
echo_r(`Exception cloning ${repo}: ${e.message}`); | |
} | |
} | |
echo_g('\nFinished cloning all repositories.'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment