This repository was archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (154 loc) · 4.6 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
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
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
"use strict";
const axios = require("axios");
const commandLineArgs = require("command-line-args");
const inquirer = require("inquirer");
const chalk = require("chalk");
const pjson = require("./package.json");
const optionDefinitions = [
{ name: "user", alias: "u", type: String },
{ name: "repos", alias: "r", type: Boolean },
{ name: "version", alias: "v", type: Boolean }
];
const MAX_WIDTH = 50;
const LABELS = {
name: "Name: ",
github: "GitHub: ",
website: "Website: ",
company: "Company: ",
location: "Location: ",
hireable: {
hireable: "This user is currently available for hire",
notHireable: "This user is currently not available for hire"
},
followers: "# of Followers: ",
topRepos: "Top 3 Repos:"
};
const options = commandLineArgs(optionDefinitions);
if (options.version) {
console.log(pjson.version);
process.exit(0);
}
function promptAndGetUserName() {
if (options.user)
return Promise.resolve({
userName: options.user,
showRepos: options.repos
});
return inquirer
.prompt([
{
type: "input",
name: "username",
message: "Enter a username >"
}
])
.then(resp => {
return { userName: resp.username, showRepos: options.repos };
});
}
promptAndGetUserName().then(payload => {
const { userName, showRepos } = payload;
axios
.get(`https://api.github.com/users/${userName}`)
.then(resp => resp.data)
.then(userData => {
if (showRepos) {
return axios
.get(`https://api.github.com/users/${userName}/repos`)
.then(resp => resp.data)
.then(payload => {
const res = payload.sort((a, b) => {
return b.stargazers_count - a.stargazers_count;
});
return res;
})
.then(repos => {
return { userData, repos, showRepos };
});
} else {
return { userData, repos: null, showRepos };
}
})
.then(data => {
const { userData, repos, showRepos } = data;
const lines = [];
const {
name,
html_url,
company,
login,
location,
hireable,
blog,
followers
} = userData;
// NAME
const gitHubName = name || login || "-";
lines.push(
chalk.cyan.bold(LABELS.name) +
gitHubName.padStart(MAX_WIDTH - LABELS.name.length)
);
// PROFILE
const githubUrl = html_url || "-";
lines.push(
chalk.cyan.bold(LABELS.github) +
githubUrl.padStart(MAX_WIDTH - LABELS.github.length)
);
// PERSONAL WEBSITE
const blogUrl = blog || "-";
lines.push(
chalk.cyan.bold(LABELS.website) +
blogUrl.padStart(MAX_WIDTH - LABELS.website.length)
);
// COMPANY
const companyName = company || "-";
lines.push(
chalk.cyan.bold(LABELS.company) +
companyName.padStart(MAX_WIDTH - LABELS.company.length)
);
// LOCATION
const githubLocation = location || "-";
lines.push(
chalk.cyan.bold(LABELS.location) +
githubLocation.padStart(MAX_WIDTH - LABELS.location.length)
);
// FOLLOWERS
const ghFollowers = followers || "-";
lines.push(" ".repeat(MAX_WIDTH));
lines.push(
chalk.cyan.bold(LABELS.followers) +
ghFollowers.toString().padStart(MAX_WIDTH - LABELS.followers.length)
);
// HIRE STATUS
const hireMessage = hireable
? chalk.green.bold(LABELS.hireable.hireable.padEnd(MAX_WIDTH))
: chalk.red.bold(LABELS.hireable.notHireable.padEnd(MAX_WIDTH));
lines.push(" ".repeat(MAX_WIDTH));
lines.push(hireMessage);
console.log(" ".repeat(5) + "-".repeat(MAX_WIDTH + 2));
lines.forEach(line => console.log(" ".repeat(4) + `| ${line} |`));
console.log(" ".repeat(5) + "-".repeat(MAX_WIDTH + 2));
lines.push(" ".repeat(MAX_WIDTH));
lines.push(" ".repeat(MAX_WIDTH));
if (showRepos) {
console.log(
" ".repeat(4) +
`| ${chalk.cyan.bold(LABELS.topRepos.padEnd(MAX_WIDTH))} |`
);
lines.push(" ".repeat(MAX_WIDTH));
repos.slice(0, 5).forEach(repo => {
const label = `${repo.name} - ${repo.stargazers_count} ⭐ `;
console.log(
" ".repeat(4) +
`| ${chalk.green.bold(`${label}`) +
repo.html_url.padStart(MAX_WIDTH - label.length)} |`
);
});
}
})
.catch(err => {
console.error(err);
console.error(`Something went wrong loading ${username}, do they exist?`);
});
});