wrap-git
Version:
Wraps GitHub profile and provides summarized details about repos, commits and language coverages for a given profile.
61 lines (60 loc) • 2 kB
JavaScript
import { getAllRepos, getRepoLngs } from "../api/repos.js";
import { throttleAll } from "../utils/throttleAll.js";
const getTopLanguages = (lngCoverage) => {
const sortedLngCoverage = [...lngCoverage].sort((a, b) => b.coverage - a.coverage);
return sortedLngCoverage.slice(0, 5);
};
export const lngSummarizer = async (username, token) => {
const allRepos = await getAllRepos(username, token);
const urls = allRepos
.filter((repo) => repo.owner === username)
.map((repo) => repo.url);
const lngStats = [];
let totalCodeSize = 0;
const languages = await throttleAll(urls, 10, async (url) => {
return await getRepoLngs(url, token);
});
// calc coverage of each language
for (const language of languages) {
if (!language)
continue;
for (const [lng, bytes] of Object.entries(language)) {
const existing = lngStats.find((stat) => stat.lngName === lng);
if (existing) {
existing.coverage += bytes;
}
else {
lngStats.push({
lngName: lng,
coverage: bytes,
repos: 0
});
}
totalCodeSize += bytes;
}
}
// cal no of repos for each lng
for (const repo of allRepos) {
const existing = lngStats.find((stat) => stat.lngName === repo.language);
if (existing) {
existing.repos += 1;
}
}
/*
* Estimated LOC = Code size in bytes / Average bytes per line
* Let Average Bytes per Line = 70 bytes/line
*
*/
const loc = Math.ceil(totalCodeSize / 70);
const allLngs = lngStats.map((stat) => ({
...stat,
coverage: Math.round((stat.coverage / totalCodeSize) * 1000) / 10
}));
const top5Lngs = getTopLanguages(allLngs);
return {
totLineOfCode: loc,
totalLngs: lngStats.length,
top5Lngs,
allLngs
};
};