Github Repo Language Stats
/**
*
* ENDPOINT USAGE
* - https://untitled-t5h6cojkxe5y.runkit.sh/:owner/:repo/
* - https://untitled-t5h6cojkxe5y.runkit.sh/:owner/:repo/:language
*
* EXAMPLE
* - https://untitled-t5h6cojkxe5y.runkit.sh/zeit/now-cli
* - https://untitled-t5h6cojkxe5y.runkit.sh/zeit/now-cli/TypeScript
*
* EXAMPLE (with Badgen)
* - https://badgen.net/runkit/untitled-t5h6cojkxe5y/zeit/now-cli
* - https://badgen.net/runkit/untitled-t5h6cojkxe5y/zeit/now-cli/TypeScript
* - https://badgen.net/runkit/untitled-t5h6cojkxe5y/zeit/now-cli/JavaScript
*/
const axios = require('axios')
const { send } = require('micro')
exports.endpoint = async function (req, res) {
const [owner, repo, lang] = req.url.replace('/', '').split('/')
if (!repo) {
return send(res, 200, {
subject: 'error',
status: 'args incomplete',
color: 'grey'
})
}
const endpoint = `https://api.github.com/repos/${owner}/${repo}/languages`
const stats = await axios(endpoint).then(res => res.data)
// list languages
if (!lang) {
return send(res, 200, {
subject: 'languages',
status: Object.keys(stats).join(' | '),
color: 'blue'
})
}
// show lang stats
send(res, 200, {
subject: lang,
status: countPercent(lang, stats) * 100 + '%',
color: 'blue'
})
}
function countPercent (lang, stats) {
const total = Object.values(meta).reduce((accu, curr) => accu + curr)
const percent = (meta[lang] || 0) / total
return percent.toFixed(2)
}
no comments