-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
131 lines (115 loc) · 3.31 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Dependencies
var GitHubColors = require("github-colors")
, GitHub = require("gh.js")
;
/**
* GhPolyglot
* Creates a new instance of `GhPolyglot`.
*
* @name GhPolyglot
* @function
* @param {String} input The repository full name
* (e.g. `"IonicaBizau/gh-polyglot"`) or the username (e.g. `"IonicaBizau"`).
* @param {String} token An optional GitHub token used for making
* authenticated requests.
* @param {String} host An optional alternative Github FQDN (e.g. `"https://github.myenterprise.com/api/v3/"`)
* @return {GhPolyglot} The `GhPolyglot` instance.
*/
function GhPolyglot (input, token, host) {
var splits = input.split("/");
this.user = splits[0];
this.repo = splits[1];
this.full_name = input;
this.gh = new GitHub({ token: token, host: host });
}
/**
* getAllRepos
* Gets all user's repositories.
*
* @name getAllRepos
* @function
* @param {Function} callback The callback function.
* @return {Request} The request object.
*/
GhPolyglot.prototype.getAllRepos = function (callback) {
var self = this;
return self.gh.get("users/" + self.user + "/repos", {
all: true
}, function (err, repos) {
if (err) {
return callback(err);
}
repos = repos.filter(function (c) {
return !c.fork;
});
if (!repos.length) {
callback(new Error("This user doesn't have any repositories."));
}
callback(null, repos);
});
};
/**
* repoStats
* Gets repository stats.
*
* @name repoStats
* @function
* @param {Function} callback The callback function.
* @return {Request} The request object.
*/
GhPolyglot.prototype.repoStats = function (callback) {
return this.gh.get("repos/" + this.full_name + "/languages", this.check(callback));
};
/**
* userStats
* Gets user stats.
*
* @name userStats
* @function
* @param {Function} callback The callback function.
* @return {Request} The request object.
*/
GhPolyglot.prototype.userStats = function (callback) {
return this.getAllRepos(this.check(callback));
};
/**
* check
* Wraps the callback in another function to manipulate the data.
*
* @name check
* @function
* @param {Function} callback The callback function.
* @return {Function} The wrapping function which gets the `err` and `data`
* arguments and changes the data converting it into an array.
*/
GhPolyglot.prototype.check = function (callback) {
return function (err, data, res) {
if (err) { return callback(err); }
var languages = {}
, arrData = []
;
if (Array.isArray(data)) {
data.forEach(function (c) {
languages[c.language] = languages[c.language] || 0;
++languages[c.language];
});
if (languages["null"]) {
languages.Others = languages["null"];
delete languages["null"];
}
} else {
languages = data;
}
arrData = Object.keys(languages).map(function (cLang) {
return {
label: cLang
, value: languages[cLang]
, color: GitHubColors.get(cLang, true).color
};
});
callback(null, arrData.sort(function (a, b) {
return a.value < b.value ? 1 : -1;
}));
};
};
module.exports = GhPolyglot;