git-year-wrapped/bin/git-year-wrapped.mjs
2026-11-26 17:43:53 +03:00

183 lines
5.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
import { writeFileSync } from "node:fs";
import { resolve } from "node:path";
import { cloneBare, cleanup } from "../lib/clone.mjs";
import { readCommits } from "../lib/log.mjs";
import { filterByWindow, computeStats } from "../lib/stats.mjs";
import { renderCard } from "../lib/card.mjs";
import { repoLabelFromUrl } from "../lib/repo-label.mjs";
const HELP = `git-year-wrapped <repo-url> [options]
Generate a shareable year-in-review card from any public git repo's commit
history - GitHub, GitLab, Rithub, or any other host that speaks plain git.
No signup, no API keys, no telemetry; runs entirely on your machine.
Options:
--year <YYYY> Only count commits from this calendar year (UTC).
Default: the current year, falling back to all-time
history if that year has no commits in this repo.
--since-days <N> Only count commits from the last N days instead of a
calendar year.
--all-time Count the repo's entire history.
--out <path> Where to write the SVG card. Default:
./<repo>-wrapped.svg
--accent <#hex> Accent color for the card. Default: #4f8cff
--json Also print the computed stats as JSON.
-h, --help Show this help.
-v, --version Show the version.
Examples:
npx git-year-wrapped https://github.com/facebook/react
npx git-year-wrapped https://rithub.dev/caelum0x/git-year-wrapped --year 2026
npx git-year-wrapped git@gitlab.com:group/project.git --all-time --json
`;
function parseArgs(argv) {
const args = { repoUrl: null, year: null, sinceDays: null, allTime: false, out: null, accent: null, json: false };
const rest = [...argv];
while (rest.length > 0) {
const token = rest.shift();
switch (token) {
case "-h":
case "--help":
process.stdout.write(HELP);
process.exit(0);
break;
case "-v":
case "--version": {
process.stdout.write("1.0.0\n");
process.exit(0);
break;
}
case "--year":
args.year = Number(rest.shift());
break;
case "--since-days":
args.sinceDays = Number(rest.shift());
break;
case "--all-time":
args.allTime = true;
break;
case "--out":
args.out = rest.shift();
break;
case "--accent":
args.accent = rest.shift();
break;
case "--json":
args.json = true;
break;
default:
if (!args.repoUrl && !token.startsWith("-")) {
args.repoUrl = token;
} else {
throw new Error(`Unknown argument: ${token}`);
}
}
}
return args;
}
function fail(message) {
process.stderr.write(`git-year-wrapped: ${message}\n`);
process.exit(1);
}
function main() {
let args;
try {
args = parseArgs(process.argv.slice(2));
} catch (err) {
fail(err.message);
return;
}
if (!args.repoUrl) {
process.stdout.write(HELP);
process.exit(1);
return;
}
const repoLabel = repoLabelFromUrl(args.repoUrl);
let gitDir;
try {
gitDir = cloneBare(args.repoUrl);
const commits = readCommits(gitDir);
let window;
let windowLabel;
if (args.allTime) {
window = {};
windowLabel = "All-time";
} else if (args.sinceDays) {
window = { sinceDays: args.sinceDays };
windowLabel = `Last ${args.sinceDays} days`;
} else {
const year = args.year || new Date().getUTCFullYear();
const inYear = filterByWindow(commits, { year });
if (inYear.length === 0 && !args.year) {
// No activity in the current calendar year yet (or a repo whose
// history predates it) - fall back to all-time rather than
// handing a stranger an empty card.
window = {};
windowLabel = "All-time";
} else {
window = { year };
windowLabel = `${year} Wrapped`;
}
}
const windowed = filterByWindow(commits, window);
const stats = computeStats(windowed);
if (stats.totalCommits === 0) {
fail(`No commits found for "${repoLabel}" in that window.`);
return;
}
const svg = renderCard({
repoLabel,
windowLabel,
stats,
accent: args.accent || undefined,
});
const outPath = resolve(args.out || `${repoLabel.replace(/[\\/]/g, "-")}-wrapped.svg`);
writeFileSync(outPath, svg, "utf8");
console.log(`${repoLabel} — ${windowLabel}`);
console.log(` commits: ${stats.totalCommits}`);
console.log(` active days: ${stats.activeDays}`);
console.log(` longest streak: ${stats.longestStreak} day(s)`);
console.log(` busiest day: ${stats.busiestWeekday ?? "—"}`);
console.log(` busiest hour UTC: ${stats.busiestHour ?? "—"}:00`);
console.log(
` top languages: ${stats.topLanguages.map((l) => l.language).join(", ") || "—"}`
);
console.log(
` top contributor: ${
stats.topContributors[0]
? `${stats.topContributors[0].name} (${stats.topContributors[0].count} commits)`
: "—"
}`
);
console.log("");
console.log(`Card written to ${outPath}`);
if (args.json) {
console.log(JSON.stringify({ repoLabel, windowLabel, stats }, null, 2));
}
} catch (err) {
fail(err.message || String(err));
} finally {
if (gitDir) cleanup(gitDir);
}
}
main();