-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstargazed.js
87 lines (69 loc) · 2.29 KB
/
stargazed.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
import fs from "node:fs";
import dotenv from "dotenv";
import { Octokit } from "@octokit/rest";
import { Command } from "commander";
dotenv.config();
const program = new Command();
program
.option(
"-u, --username <username>",
"GitHub username",
process.env.GITHUB_USERNAME,
)
.option("-t, --token <token>", "GitHub token", process.env.GITHUB_TOKEN)
.option("-m, --message <message>", "Commit message", "Update starred repos")
.parse();
const { username, token, message } = program.opts();
if (!username || !token) {
console.error(
"⚠️ Missing required environment variables (GITHUB_USERNAME or GITHUB_TOKEN).",
);
process.exit(1);
}
const octokit = new Octokit({ auth: token });
async function getStarredRepos(username) {
const repos = [];
let page = 1;
while (true) {
const { data } = await octokit.activity.listReposStarredByUser({
username,
per_page: 100,
page,
});
if (data.length === 0) break;
repos.push(...data);
page++;
}
return repos;
}
async function generate() {
try {
console.log(`🔍 Fetching starred repositories for ${username}...`);
const starredRepos = await getStarredRepos(username);
const outputJsonData = starredRepos.map((repo) => ({
name: repo.name || "Unknown Repo",
description: repo.description || "No description",
author: repo.owner?.login || "Unknown Author",
stars: repo.stargazers_count || 0,
url: repo.html_url || "#",
date: new Date(repo.created_at || Date.now()).toLocaleDateString(),
languages: repo.language ? [{ language: repo.language }] : [],
topics: repo.topics || [],
license: repo.license?.spdx_id || "None",
forks: repo.forks_count || 0,
open_issues: repo.open_issues_count || 0,
last_updated: new Date(repo.pushed_at || Date.now()).toLocaleDateString(),
}));
fs.writeFileSync("data.json", JSON.stringify(outputJsonData, null, 2));
const readmeContent = `
# My GitHub Stars
Last updated: ${new Date().toLocaleDateString()}
Automatically generated by [stargazed](https://github.com/KBLLR/git-stars).
`;
fs.writeFileSync("README.md", readmeContent);
console.log("✅ README.md and data.json generated successfully!");
} catch (error) {
console.error("⚠️ Error:", error);
}
}
generate();