|
1 | 1 | import fs from "fs"; |
2 | 2 | import chalk from "chalk"; |
3 | 3 | import simpleGit from "simple-git"; |
| 4 | +import { execSync } from "child_process"; |
| 5 | + |
| 6 | +const fetchFn = global.fetch || (await import("node-fetch")).default; |
4 | 7 |
|
5 | 8 | export async function projectInfo() { |
6 | | - const pkgPath = "./package.json"; |
| 9 | + try { |
| 10 | + const remoteUrl = execSync("git config --get remote.origin.url").toString().trim(); |
7 | 11 |
|
8 | | - const git = simpleGit(); |
9 | | - const currentBranch = (await git.branchLocal()).current; |
10 | | - const lastCommit = (await git.log({ n: 1 })).latest; |
| 12 | + if (!remoteUrl) { |
| 13 | + console.log(chalk.red("❌ No remote Git repository found.")); |
| 14 | + return; |
| 15 | + } |
11 | 16 |
|
12 | | - if (fs.existsSync(pkgPath)) { |
13 | | - const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8")); |
14 | | - console.log(chalk.cyan("📦 Project Info")); |
15 | | - console.log(chalk.white(`Name: ${pkg.name}`)); |
16 | | - console.log(chalk.white(`Version: ${pkg.version}`)); |
17 | | - } |
| 17 | + const match = remoteUrl.match(/github\.com[:/](.+?)\/(.+?)(?:\.git)?$/); |
| 18 | + if (!match) { |
| 19 | + console.log(chalk.red("❌ Repository does not appear to be on GitHub.")); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + const [_, owner, repo] = match; |
| 24 | + |
| 25 | + const res = await fetchFn(`https://api.github.com/repos/${owner}/${repo}`); |
| 26 | + if (!res.ok) { |
| 27 | + console.log(chalk.red(`❌ Unable to retrieve info from GitHub (${res.status})`)); |
| 28 | + return; |
| 29 | + } |
18 | 30 |
|
19 | | - console.log(chalk.white(`Branch: ${currentBranch}`)); |
20 | | - console.log(chalk.white(`Last commit: ${lastCommit.message}`)); |
21 | | - console.log(chalk.gray(`By ${lastCommit.author_name} on ${lastCommit.date}`)); |
| 31 | + const data = await res.json(); |
| 32 | + |
| 33 | + console.log(chalk.bold.cyan(`\n📦 Repository Info: ${data.full_name}`)); |
| 34 | + console.log(chalk.gray(data.html_url)); |
| 35 | + console.log(""); |
| 36 | + console.log(`${chalk.yellow("🪶 Description:")} ${data.description || "—"}`); |
| 37 | + console.log(`${chalk.yellow("⭐ Stars:")} ${data.stargazers_count}`); |
| 38 | + console.log(`${chalk.yellow("🍴 Forks:")} ${data.forks_count}`); |
| 39 | + console.log(`${chalk.yellow("👀 Open Issues:")} ${data.open_issues_count}\n`); |
| 40 | + console.log(`${chalk.yellow("🧱 Language:")} ${data.language || "Unknown"}`); |
| 41 | + console.log(`${chalk.yellow("🕒 Last Updated:")} ${new Date(data.updated_at).toLocaleString()}`); |
| 42 | + |
| 43 | + const commitsRes = await fetchFn(`https://api.github.com/repos/${owner}/${repo}/commits?per_page=1`); |
| 44 | + if (commitsRes.ok) { |
| 45 | + const [lastCommit] = await commitsRes.json(); |
| 46 | + if (lastCommit) { |
| 47 | + console.log(`${chalk.yellow("🧩 Last Commit:")} ${lastCommit.commit.message}`); |
| 48 | + console.log(`${chalk.gray(`by ${lastCommit.commit.author.name} on ${lastCommit.commit.author.date}`)}\n`); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + } catch (err) { |
| 53 | + console.error(chalk.red(`❌ Error: ${err.message}`)); |
| 54 | + } |
22 | 55 | } |
0 commit comments