Skip to content

Instantly share code, notes, and snippets.

@MCJack123
Last active September 26, 2022 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MCJack123/b8ed423cd55814dd884a6b5884af16f3 to your computer and use it in GitHub Desktop.
Save MCJack123/b8ed423cd55814dd884a6b5884af16f3 to your computer and use it in GitHub Desktop.
Script to calculate total language size stats for a user, including Gists
// Usage: node gh-lang-count.mjs <user> [PAK]
import * as process from 'process';
if (!global.fetch) throw new Error("This requires NodeJS 17.5.0 or later.");
if (process.argv.length < 3) throw new Error("Usage: gh-lang-count <user> [PAK]");
const username = process.argv[2];
let key = undefined;
if (process.argv.length > 3) key = "token " + process.argv[3];
async function main() {
console.log("Listing repos...");
let repos = [];
let page = 1;
do {
const list = await (await fetch(`https://api.github.com/users/${username}/repos?per_page=100&page=${page++}`, {headers: {"Authorization": key}})).json();
if (!Array.isArray(list)) throw new Error(list.message);
repos = repos.concat(list);
} while (repos.length % 100 == 0);
process.stdout.write(`Processing repos... (0 / ${repos.length})`);
let langStats = {};
let sum = 0;
page = 1;
for (const repo of repos) {
const langs = await (await fetch(repo.languages_url, {headers: {"Authorization": key}})).json();
for (const l in langs) {
if (typeof langStats[l] !== "number") langStats[l] = 0;
langStats[l] += langs[l];
sum += langs[l];
}
process.stdout.write(`\rProcessing repos... (${page++} / ${repos.length})`);
}
process.stdout.write("\n");
console.log("Listing Gists...");
let gists = [];
page = 1;
do {
const list = await (await fetch(`https://api.github.com/users/${username}/gists?per_page=100&page=${page++}`, {headers: {"Authorization": key}})).json();
if (!Array.isArray(list)) throw new Error(list.message);
gists = gists.concat(list);
} while (gists.length % 100 == 0);
process.stdout.write(`Processing Gists... (0 / ${gists.length})`);
page = 1;
for (const gist of gists) {
for (const filename in gist.files) {
const file = gist.files[filename];
const l = file.language;
if (typeof l !== "string") continue;
if (typeof langStats[l] !== "number") langStats[l] = 0;
langStats[l] += file.size;
sum += file.size;
}
process.stdout.write(`\rProcessing Gists... (${page++} / ${gists.length})`);
}
process.stdout.write("\n\n");
const stats = [];
for (const l in langStats) {
let pct = langStats[l] / sum * 100;
if (pct >= 10) pct = pct.toFixed(1);
else if (pct >= 0.01) pct = pct.toFixed(2);
else pct = pct.toExponential(2);
stats.push({"Language": l, "Size (bytes)": langStats[l], "% of all code": pct + "%"});
}
stats.sort((a, b) => b["Size (bytes)"] - a["Size (bytes)"]);
console.log(`Languages statistics for ${username}:`);
console.table(stats);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment