-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit_clone_org
executable file
·58 lines (49 loc) · 1.64 KB
/
git_clone_org
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
#! /usr/local/bin/node
var exec = require('child_process').exec;
var readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function Cloner() {
readline.question('Enter your organization name: ', this.onOrgName.bind(this));
};
Cloner.prototype.onOrgName = function(orgName) {
this.orgName = orgName;
readline.question('Where would you like the org cloned to? ', this.onPath.bind(this));
readline.write(process.cwd());
}
Cloner.prototype.onPath = function(path) {
this.path = path;
readline.question('Enter your personal access token (https://github.com/settings/applications#personal-access-tokens): ', this.onAccessToken.bind(this));
}
Cloner.prototype.onAccessToken = function(accessToken) {
readline.close();
var _this = this;
try {
process.chdir(this.path);
}
catch(e) {
process.stdout.write('chdir: ' + e);
return 0;
}
var apiCmd = 'curl -sS -u ' + accessToken + ':x-oauth-basic https://api.github.com/orgs/' + this.orgName + '/repos';
exec(apiCmd, function(error, stdout, stderr) {
_this.repos = JSON.parse(stdout);
if(_this.repos.length) {
_this.cloneRepo(0);
}
else {
process.stdout.write("No available repos.\n");
}
});
}
Cloner.prototype.cloneRepo = function(index) {
var _this = this;
var repo = _this.repos[index];
process.stdout.write("Cloning " + repo.name + "\n");
exec('git clone ' + repo.ssh_url, function(error, stdout, stderr) {
if(error) process.stderr.write("Error cloning " + repo.name + ": " + error + "\n");
if(index < _this.repos.length - 1) _this.cloneRepo(index + 1);
});
}
module.exports = new Cloner();