Skip to content

Instantly share code, notes, and snippets.

@fvilante
Created August 6, 2022 07:27
Show Gist options
  • Save fvilante/6a2ebc2c9f21047f874780a333b6d1e4 to your computer and use it in GitHub Desktop.
Save fvilante/6a2ebc2c9f21047f874780a333b6d1e4 to your computer and use it in GitHub Desktop.
Get the estimated size (in number of lines) of a given GitHub repository
// to be used with deno run-time
// the first argument must be the repo name
// example: deno run --allow-net .\github_repolines.js microsoft/typescript
async function countGithub(repo) {
const repoAddress = `https://github.com/${repo}`;
const repoApi = `https://api.github.com/repos/${repo}` + `/stats/contributors`;
const response = await fetch(repoApi);
const contributors = await response.json();
const lineCounts = contributors.map(contributor => (
contributor.weeks.reduce((lineCount, week) => lineCount + week.a - week.d, 0)
));
const lines = lineCounts.reduce((lineTotal, lineCount) => lineTotal + lineCount);
console.table({
repository: repo,
address: repoAddress,
api: repoApi,
linesOfCode: lines,
});
}
countGithub(Deno.args[0]); // or count anything you like
@fvilante
Copy link
Author

fvilante commented Aug 6, 2022

If you have deno installed, just copy and paste line below in your terminal:
(Works on Windows, Mac and Linux).

The words after ".js" represents the repo organization and name respectivelly, and separeted by a slash.

deno run --allow-net https://gist.githubusercontent.com/fvilante/6a2ebc2c9f21047f874780a333b6d1e4/raw/a53cadd529434d1c961454c29b7791873e38352e/github_repolines.js microsoft/typescript

@fvilante
Copy link
Author

fvilante commented Aug 6, 2022

Inspirated in this stackoverflow question.

I'm not sure if the computated number of lines includes or excludes doc files for examples, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment