-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
113 lines (103 loc) · 3.01 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var spawn = require("child_process").spawn;
//noinspection JSUnresolvedFunction
var request = require("request").defaults({ jar: true });
var cwd = process.cwd();
var User = function (options) {
this.BASE_URI = "https://api.github.com/users/";
this.User = options.User;
this.username = options.username;
this.password = options.password;
this.token = options.token;
this.gitaccess = options.gitaccess;
this.gitsettings = options.gitsettings;
this.type = options.type;
};
User.prototype.getRequestUri = function () {
// also possible to use &token=
// for /orgs/:org/repos type: all, public, private, forks, sources, member
// /users/:username/repos type: all, owner, member
return this.BASE_URI + this.User + "/repos?type=owner";
//return this.BASE_URI + this.User + '/repos' + '?type=' + this.type;
};
User.prototype.getRepositories = function (callback) {
var options = {
url: this.getRequestUri() + "&per_page=100",
headers: {
"User-Agent": "request",
},
json: true,
};
if (this.token) {
options.headers["Authorization"] = "token " + this.token;
} else {
options["auth"] = {
user: this.username,
pass: this.password,
sendImmediately: true,
};
}
console.info(
"Requesting github repositories for " +
this.User +
" with url " +
options.url +
" ..."
);
request.get(
options,
function (error, response, body) {
if (error) {
console.error(error);
callback.call(this, error);
} else {
if (response.statusCode >= 300) {
console.error(response.statusCode + " " + body.message);
console.log(
"Maybe your access-token has expired!? Reinstall (npm install clone-all-github-repos -g) to create a new config file."
);
process.exit(-1);
}
for (var i = 0; i < body.length; i++) {
this.executeCloneCommand(body[i]);
}
callback.call(this, null, { sucess: true });
}
}.bind(this)
);
};
User.prototype.getCloneUrl = function (repo) {
var url;
switch (this.gitaccess) {
case "ssh":
//noinspection JSUnresolvedVariable
url = repo.ssh_url;
break;
case "git": //remove, you cannot push on git urls
//noinspection JSUnresolvedVariable
url = repo.git_url;
break;
case "https":
//noinspection JSUnresolvedVariable
url = repo.clone_url;
break;
default:
return console.error(
"Unknown git access protocol provided: " + this.gitaccess
);
}
return url;
};
User.prototype.executeCloneCommand = function (repo) {
var url = this.getCloneUrl(repo);
var spawnParams = ["clone"].concat(this.gitsettings || [], url);
console.info("cloning " + url);
var process = spawn("git", spawnParams, { cwd: cwd });
process.on("close", function (status) {
if (status == 0) {
console.info("success cloning " + url);
} else {
console.error("git clone failed with status " + status);
}
});
};
module.exports = User;