forked from soti84/clone_all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_clone_all.js
82 lines (61 loc) · 1.99 KB
/
github_clone_all.js
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
// clone all github user repos
// TECNO 2020 [email protected]
// --------------------------------
// How its work
// 1- get user repos from github api
// 2- clone each one in loop
// --------------------------------
const https = require('https');
const shell = require('shelljs');
module.exports.username_clone_all = function(username) {
if(username)
username_clone_all(username);
else
console.log("'username' required !!");
return;
}
function username_clone_all(username) {
console.log(`will clone all "${username}" repos ... enjoy`);
var options = {
hostname: "api.github.com",
port: 443, //https
path: `/users/${username}/repos?per_page=9999`,
method: 'GET',
headers: {
'content-type': 'application/json',
'User-Agent': 'Mozilla/5.0'
}
};
//change to http for local testing
var req = https.get(options, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body = body + chunk;
});
res.on('end', function() {
repos = JSON.parse(body);
console.log(` (${repos.length}) repos found !!`);
//create user folder
shell.mkdir(username);
for (var i = 0; i < repos.length; i++) {
console.log((i + 1) + '- ' + repos[i].name);
shell.exec(`git clone ${repos[i].clone_url} ./${username}/${repos[i].name}`);
console.log(repos[i].name + ' done');
console.log('--------------');
}
console.log('Done !!!!');
if (res.statusCode != 200) {
console.log("Api call failed with response code " + res.statusCode);
return;
} else {
return;
}
});
});
req.on('error', function(e) {
console.log("Error : " + e.message);
callback(e);
});
req.end();
}