-
Notifications
You must be signed in to change notification settings - Fork 0
/
Github.ts
158 lines (127 loc) · 3.54 KB
/
Github.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import yargs from "https://deno.land/x/[email protected]/deno.ts";
// Yeah I'm lazy af
const print = console.log;
const BASE_URL = "https://api.github.com/users/";
type GthUserItem = {
count: number;
arr: string[];
};
class GthUser {
name: string = "";
bio: string = "";
link: string = "";
repos: GthUserItem = { count: 0, arr: [] };
gists: GthUserItem = { count: 0, arr: [] };
followers: GthUserItem = { count: 0, arr: [] };
following: GthUserItem = { count: 0, arr: [] };
constructor(public username: string) {}
static async create(user: string): Promise<GthUser> {
let self = new GthUser(user);
let url = BASE_URL + self.username;
let res = await fetch(url);
if (!res.ok)
throw new Error(`can't fetch url '${url}'`);
let obj = await res.json();
self.name = obj.name;
self.bio = obj.bio;
self.link = obj.html_url;
self.repos.count = obj.public_repos;
self.gists.count = obj.public_gists;
self.followers.count = obj.followers;
self.following.count = obj.following;
return self;
}
async fetch(thing: string) {
let url = `${BASE_URL + this.username}/${thing}`
let res = await fetch(url);
if (!res.ok)
throw new Error(`can't fetch url '${url}'`);
let arr = await res.json();
switch (thing) {
case "repos":
for (const val of arr)
this.repos?.arr.push(val.name);
break;
case "gists":
for (const val of arr)
this.gists?.arr.push(val.description);
break;
case "followers":
for (const val of arr)
this.followers?.arr.push(val.login);
break;
case "following":
for (const val of arr)
this.following?.arr.push(val.login);
break;
default:
throw new Error(`unsupported endpoint: ${thing}`);
}
}
}
let opts = yargs(Deno.args)
.version(false)
.usage("Github [options...] <usernames...>")
.option("repos", {
alias: "r",
type: "boolean",
describe: "Show user repos"
})
.option("gists", {
alias: "g",
type: "boolean",
describe: "Show user gists"
})
.option("followers", {
alias: "f",
type: "boolean",
describe: "Show user followers"
})
.option("following", {
alias: "F",
type: "boolean",
describe: "Show user following"
})
.help("help", "Show this help")
.alias("h", "help")
.parse();
if (opts._.length <= 0) {
print("Nothing to do.");
print("Pass -h or --help to see the help.");
Deno.exit(1);
}
for (const val of opts._) {
let user = await GthUser.create(val);
print(`Name: ${user.name}`);
print(`Bio: ${user.bio}`);
print(`Link: ${user.link}`);
print(`Public repos: ${user.repos?.count}`);
if (opts.r) {
await user.fetch("repos");
if (user.repos)
for (const idx in user.repos.arr)
print(` | ${parseInt(idx) + 1} ${user.repos.arr[parseInt(idx)]}`);
}
print(`Public gists: ${user.gists?.count}`);
if (opts.g) {
await user.fetch("gists");
if (user.gists)
for (const idx in user.gists.arr)
print(` | ${parseInt(idx) + 1} ${user.gists.arr[parseInt(idx)]}`);
}
print(`Public followers: ${user.followers?.count}`);
if (opts.followers) {
await user.fetch("followers");
if (user.followers)
for (const val of user.followers.arr)
print(` | @${val}`);
}
print(`Public following: ${user.following?.count}`);
if (opts.following) {
await user.fetch("following");
if (user.following)
for (const val of user.following.arr)
print(` | @${val}`);
}
print();
}