2020-05-16 16:17:49 +00:00
|
|
|
import cheerio from 'cheerio'
|
|
|
|
import got from '../libs/got'
|
|
|
|
import { millify, version, versionColor } from '../libs/utils'
|
|
|
|
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
|
|
|
|
|
|
|
const OPAM_REPO_URL = 'https://opam.ocaml.org/packages/'
|
|
|
|
|
|
|
|
type PackageInfo = {
|
|
|
|
version: string,
|
|
|
|
license: string,
|
|
|
|
downloads: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export default createBadgenHandler({
|
|
|
|
title: 'OCaml Package Manager',
|
|
|
|
examples: {
|
|
|
|
'/opam/v/merlin': 'version',
|
|
|
|
'/opam/v/ocamlformat': 'version',
|
|
|
|
'/opam/dm/lwt': 'monthly downloads',
|
|
|
|
'/opam/license/cohttp': 'license'
|
|
|
|
},
|
|
|
|
handlers: {
|
|
|
|
'/opam/:topic/:pkg': handler
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
async function handler ({ topic, pkg }: PathArgs) {
|
|
|
|
const html = await got(pkg, { prefixUrl: OPAM_REPO_URL }).text()
|
|
|
|
const {
|
|
|
|
downloads,
|
|
|
|
license,
|
|
|
|
version: ver
|
|
|
|
} = await getPackageInfo(html)
|
|
|
|
|
|
|
|
switch (topic) {
|
|
|
|
case 'v': {
|
|
|
|
return {
|
|
|
|
subject: 'opam',
|
|
|
|
status: version(ver),
|
|
|
|
color: versionColor(ver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'license': {
|
|
|
|
return {
|
|
|
|
subject: 'license',
|
|
|
|
status: license || 'unknown',
|
|
|
|
color: 'blue'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'dm': {
|
|
|
|
return {
|
|
|
|
subject: 'downloads',
|
|
|
|
status: millify(downloads) + '/month',
|
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPackageInfo(html: string): PackageInfo {
|
2020-05-20 08:51:04 +00:00
|
|
|
const info: PackageInfo = { version: '', license: '', downloads: NaN }
|
2020-11-08 02:39:19 +00:00
|
|
|
|
2020-05-16 16:17:49 +00:00
|
|
|
const $ = cheerio.load(html)
|
2020-05-20 08:51:04 +00:00
|
|
|
const text = (selector: any) => $(selector).text().trim()
|
|
|
|
|
|
|
|
info.version = text($('.package-version').first())
|
|
|
|
$('.package-info th').filter((_, el) => {
|
2020-05-16 16:17:49 +00:00
|
|
|
const $el = $(el)
|
2020-05-20 08:51:04 +00:00
|
|
|
const label = text($el).toLowerCase()
|
|
|
|
if (label === 'license') {
|
|
|
|
info.license = text($el.next())
|
|
|
|
} else if (label === 'statistics') {
|
|
|
|
info.downloads = parseInt(text($el.next().find('strong')), 0)
|
2020-05-16 16:17:49 +00:00
|
|
|
}
|
2020-05-20 08:51:04 +00:00
|
|
|
return !info.license && isNaN(info.downloads)
|
|
|
|
})
|
|
|
|
return info
|
2020-05-16 16:17:49 +00:00
|
|
|
}
|