Repository navigation
Expand file tree
/
Copy pathdownloadAllRepos.js
More file actions
98 lines (85 loc) · 2.5 KB
/
Copy pathdownloadAllRepos.js
File metadata and controls
98 lines (85 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const shell = require('shelljs');//To clone repos, shell commands
var fs = require('fs');
const https = require('https');
var parse = require('parse-link-header');//To parse link in header
const { Agent } = require('http');
//Setting up max events listeners
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.setMaxListeners(200);
/*
* ------------------------------------------------Functions----------------------------------------------------------------------------
*/
//Function to remove already present directory
rmDir = function(dirPath) {
try { var files = fs.readdirSync(dirPath); }
catch(e) { return; }
if (files.length > 0)
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else
rmDir(filePath);
}
fs.rmdirSync(dirPath);
};
//Function to clone a Repository using shelljs
function clone(url){
if(shell.exec('git clone '+ url)==0)
console.log("Successfully Clonned "+url);
else
console.log("ERROR! Clonning "+url);
}
//Making Directory to store all cloned Repositories
var makeDirectory=(username)=>{
var dir = './Repositories_'+username;
if (!fs.existsSync(dir)){
console.log("Creating Directory "+dir)
fs.mkdirSync(dir);
}
else
{
console.log("Found Exsisting Directory "+dir+" Refreshing..")
rmDir(dir);
fs.mkdirSync(dir);
}
const path = dir;
shell.cd(path)
}
//Get all user Repositories and clone
var getRepos=(apiUrl)=>{
console.log("Getting Repo links from "+apiUrl);
const options = {
host: 'api.github.com',
path: apiUrl ,
headers: { 'User-Agent': 'Repo_script' }
};
https.get(options, function(res) {
var link=parse(res.headers.link);
if(link.next){
getRepos(link.next.url.split('.com')[1]);
}
var body='';
res.on('data', function(chunk) {
body+=chunk;
});
res.on('end',()=>{
var data=JSON.parse(body);
for(var i=0;i<data.length;i++){
clone(data[i].clone_url);//clone repo Function called
}
})
res.on('error',()=>console.log());
});
}
/*
* ------------------------------------------------Functions-End----------------------------------------------------------------------------
*/
if(process.argv[2]){
makeDirectory(process.argv[2]);
getRepos('/users/'+process.argv[2]+'/repos');
}
else{
console.log("Error! Github Username Not Provided! Write 'node repo [Your github username]'");
}