diff --git a/libs/index.md b/libs/index.md index d171e16..3555dca 100644 --- a/libs/index.md +++ b/libs/index.md @@ -177,12 +177,17 @@ Available query params: ['name', '/rubygems/n/rails'], ['platform', '/rubygems/p/rails'], ], - 'apm': [ + apm: [ ['version', '/apm/v/linter'], ['license', '/apm/license/linter'], ['downloads', '/apm/dl/linter'], ['stars', '/apm/stars/linter'], ], + hackage: [ + ['version', '/hackage/v/abt'], + ['version', '/hackage/v/Cabal'], + ['license', '/hackage/license/Cabal'] + ], /* CIs */ travis: [ ['build', '/travis/babel/babel'], diff --git a/libs/live-fns/_index.js b/libs/live-fns/_index.js index dbed8e2..45a17e9 100644 --- a/libs/live-fns/_index.js +++ b/libs/live-fns/_index.js @@ -12,6 +12,7 @@ module.exports = { 'david': require('./david.js'), 'docker': require('./docker.js'), 'github': require('./github.js'), + 'hackage': require('./hackage.js'), 'homebrew': require('./homebrew.js'), 'npm': require('./npm.js'), 'nuget': require('./nuget.js'), diff --git a/libs/live-fns/hackage.js b/libs/live-fns/hackage.js new file mode 100644 index 0000000..ba5a324 --- /dev/null +++ b/libs/live-fns/hackage.js @@ -0,0 +1,37 @@ +const axios = require('../axios.js') +const semColor = require('../utils/sem-color.js') + +module.exports = async function (topic, pkg) { + const endpoint = `https://hackage.haskell.org/package/${pkg}/${pkg}.cabal` + + const cabal = await axios.get(endpoint).then(res => res.data) + const { version, license } = parseCabalFile(cabal) + + switch (topic) { + case 'v': + return { + subject: 'cabal', + status: 'v' + version, + color: semColor(version) + } + case 'license': + return { + subject: 'license', + status: license, + color: 'blue' + } + default: + return { + status: 'unknown topic' + } + } +} + +// Naive implementation (only parse meta blocks) +function parseCabalFile (raw) { + return raw.match(/[\w-]+:.+\S+$/gm).reduce((accu, line) => { + const [key, value] = line.split(':') + accu[key] = value.trim() + return accu + }, {}) +}