badge(homebrew): add cask version & download analytics support (#378)

* Add homebrew cask support

* Add homebrew download badges
pull/383/head
Dario Vladović 2020-05-08 17:43:18 +02:00 zatwierdzone przez GitHub
rodzic 55a89ffcea
commit 1fb0354244
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 37 dodań i 10 usunięć

Wyświetl plik

@ -1,25 +1,52 @@
import got from '../libs/got'
import { version, versionColor } from '../libs/utils'
import { millify, version, versionColor } from '../libs/utils'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
export default createBadgenHandler({
title: 'Homebrew',
examples: {
'/homebrew/v/fish': 'version',
'/homebrew/v/cake': 'version'
'/homebrew/v/cake': 'version',
'/homebrew/dm/fish': 'monthly downloads',
'/homebrew/dy/fish': 'yearly downloads',
'/homebrew/cask/v/atom': 'version',
'/homebrew/cask/v/whichspace': 'version'
// NOTE: cask analytics are broken
// '/homebrew/cask/dm/atom': 'monthly downloads',
// '/homebrew/cask/dy/atom': 'yearly downloads'
},
handlers: {
'/homebrew/v/:pkg': handler
'/homebrew/:topic<v|dm|dy>/:pkg': handler,
'/homebrew/:type<formula|cask>/:topic<v|dm|dy>/:pkg': handler
}
})
async function handler ({ pkg }: PathArgs) {
const endpoint = `https://formulae.brew.sh/api/formula/${pkg}.json`
const { versions } = await got(endpoint).json<any>()
async function handler ({ type = 'formula', topic, pkg }: PathArgs) {
const endpoint = `https://formulae.brew.sh/api/${type}/${pkg}.json`
const {
analytics,
versions,
version:ver = versions.stable
} = await got(endpoint).json<any>()
return {
subject: 'homebrew',
status: version(versions.stable),
color: versionColor(versions.stable)
switch (topic) {
case 'v':
return {
subject: type === 'cask' ? 'homebrew cask' : 'homebrew',
status: version(ver),
color: versionColor(ver)
}
case 'dm':
return {
subject: 'downloads',
status: millify(analytics.install['30d'][pkg]) + '/month',
color: 'green'
}
case 'dy':
return {
subject: 'downloads',
status: millify(analytics.install['365d'][pkg]) + '/year',
color: 'green'
}
}
}