2019-05-31 07:59:03 +00:00
|
|
|
import got from '../libs/got'
|
|
|
|
import { version as v, versionColor } from '../libs/utils'
|
2019-11-18 15:52:07 +00:00
|
|
|
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
2019-05-31 07:59:03 +00:00
|
|
|
|
2019-11-18 15:52:07 +00:00
|
|
|
export default createBadgenHandler({
|
2019-05-31 07:59:03 +00:00
|
|
|
title: 'Hackage',
|
|
|
|
examples: {
|
|
|
|
'/hackage/v/abt': 'version',
|
|
|
|
'/hackage/v/Cabal': 'version',
|
|
|
|
'/hackage/license/Cabal': 'license',
|
2019-11-18 15:52:07 +00:00
|
|
|
},
|
|
|
|
handlers: {
|
|
|
|
'/hackage/:topic<v|license>/:pkg': handler
|
2019-05-31 07:59:03 +00:00
|
|
|
}
|
2019-11-18 15:52:07 +00:00
|
|
|
})
|
2019-05-31 07:59:03 +00:00
|
|
|
|
2019-11-18 15:52:07 +00:00
|
|
|
async function handler ({ topic, pkg }: PathArgs) {
|
2019-05-31 07:59:03 +00:00
|
|
|
const endpoint = `https://hackage.haskell.org/package/${pkg}/${pkg}.cabal`
|
2020-01-26 07:43:40 +00:00
|
|
|
const cabal = await got(endpoint).text()
|
2019-05-31 07:59:03 +00:00
|
|
|
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
|
|
|
|
}, {})
|
|
|
|
}
|