kopia lustrzana https://github.com/badgen/badgen.net
47 wiersze
1.2 KiB
TypeScript
47 wiersze
1.2 KiB
TypeScript
![]() |
import got from '../libs/got'
|
||
|
import { version as v, versionColor } from '../libs/utils'
|
||
![]() |
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
||
![]() |
|
||
![]() |
export default createBadgenHandler({
|
||
![]() |
title: 'Hackage',
|
||
|
examples: {
|
||
|
'/hackage/v/abt': 'version',
|
||
|
'/hackage/v/Cabal': 'version',
|
||
|
'/hackage/license/Cabal': 'license',
|
||
![]() |
},
|
||
|
handlers: {
|
||
|
'/hackage/:topic<v|license>/:pkg': handler
|
||
![]() |
}
|
||
![]() |
})
|
||
![]() |
|
||
![]() |
async function handler ({ topic, pkg }: PathArgs) {
|
||
![]() |
const endpoint = `https://hackage.haskell.org/package/${pkg}/${pkg}.cabal`
|
||
|
// @ts-ignore
|
||
|
const cabal = await got(endpoint, { json: false }).then(res => res.body)
|
||
|
const { version, license } = parseCabalFile(cabal)
|
||
|
|
||
|
switch (topic) {
|
||
|
case 'v':
|
||
|
return {
|
||
|
subject: 'hackage',
|
||
|
status: v(version),
|
||
|
color: versionColor(version)
|
||
|
}
|
||
|
case 'license':
|
||
|
return {
|
||
|
subject: 'license',
|
||
|
status: license,
|
||
|
color: 'blue'
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Naive implementation (only parse meta blocks)
|
||
|
const parseCabalFile = raw => {
|
||
|
return raw.match(/[\w-]+:.+\S+$/gm).reduce((accu, line) => {
|
||
|
const [key, value] = line.split(':')
|
||
|
accu[key] = value.trim()
|
||
|
return accu
|
||
|
}, {})
|
||
|
}
|