-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.js
More file actions
160 lines (114 loc) · 4.46 KB
/
analyze.js
File metadata and controls
160 lines (114 loc) · 4.46 KB
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
#!/usr/bin/env node
const axios = require("axios");
const chalk = require("chalk").default;
const ora = require("ora").default;
const boxen = require("boxen").default;
const Table = require("cli-table3");
const gradient = require("gradient-string");
function renderLanguageChart(languages) {
const total = Object.values(languages).reduce((a, b) => a + b, 0);
let result = "\nLanguages:\n";
for (const lang in languages) {
const percent = ((languages[lang] / total) * 100).toFixed(1);
const bars = Math.round(percent / 5);
const bar = "█".repeat(bars);
result += `${chalk.cyan(lang.padEnd(10))} ${bar} ${percent}%\n`;
}
return result;
}
async function analyzeRepo(repo) {
if (!repo || !repo.includes("/")) {
console.log(chalk.red("Format phải là owner/repo"));
process.exit(1);
}
const spinner = ora("Fetching GitHub data...").start();
try {
const repoRes = await axios.get(`https://api.github.com/repos/${repo}`);
const data = repoRes.data;
let contributors = 0;
let releases = 0;
let languages = {};
try {
const c = await axios.get(`https://api.github.com/repos/${repo}/contributors`);
contributors = c.data.length;
} catch {}
try {
const r = await axios.get(`https://api.github.com/repos/${repo}/releases`);
releases = r.data.length;
} catch {}
try {
const l = await axios.get(`https://api.github.com/repos/${repo}/languages`);
languages = l.data;
} catch {}
spinner.stop();
const table = new Table({
head: [chalk.cyan("Metric"), chalk.cyan("Value")],
colWidths: [22, 40]
});
table.push(
["Repository", chalk.yellow(data.full_name)],
["Description", data.description || "None"],
["Stars ⭐", data.stargazers_count],
["Forks 🍴", data.forks_count],
["Watchers 👀", data.watchers_count],
["Open Issues 🐞", data.open_issues_count],
["Language 💻", data.language],
["Repo Size 📦", data.size + " KB"],
["Contributors 👨💻", contributors],
["Releases 🚀", releases],
["Subscribers 👀", data.subscribers_count],
["Network 🌐", data.network_count],
["Created", new Date(data.created_at).toDateString()],
["Last Update", new Date(data.updated_at).toDateString()]
);
const title = gradient.pastel.multiline(`
GitHub Repository Analyzer
`);
console.log(title);
console.log(
boxen(table.toString(), {
padding: 1,
margin: 1,
borderStyle: "round",
borderColor: "cyan"
})
);
if (Object.keys(languages).length > 0) {
const chart = renderLanguageChart(languages);
console.log(
boxen(chart, {
padding: 1,
borderStyle: "round",
borderColor: "green"
})
);
}
} catch (err) {
spinner.stop();
console.log(chalk.red("\nGitHub API Error\n"));
if (err.response) {
console.log(chalk.yellow("Status:"), err.response.status);
if (err.response.data && err.response.data.message) {
console.log(chalk.red("Message:"), err.response.data.message);
}
if (err.response.status === 403) {
console.log(chalk.red("\nAPI rate limit exceeded."));
console.log(chalk.gray("Limit: 60 requests/hour for public API"));
console.log(chalk.gray("Use a GitHub token to increase limit."));
}
if (err.response.status === 404) {
console.log(chalk.red("\nRepository not found."));
console.log(chalk.gray("Check owner/repo format."));
}
} else {
console.log(chalk.red("Network error:"), err.message);
}
}
}
const repo = process.argv[2];
if (!repo) {
console.log(chalk.yellow("\nUsage:\n"));
console.log("node analyze.js owner/repo\n");
process.exit(0);
}
analyzeRepo(repo);