badgen.net/libs/live-fns/npm.js

39 wiersze
1.0 KiB
JavaScript
Czysty Zwykły widok Historia

2018-06-30 17:52:50 +00:00
const r2 = require('r2')
2018-07-10 03:09:38 +00:00
module.exports = async function (method, ...args) {
switch (method) {
case 'v':
return v(args)
default:
return {
subject: 'npm',
status: 'unknown',
color: 'grey'
}
}
}
async function v (args) {
2018-06-30 17:52:50 +00:00
const version = await fetchVersion(args.join('%2F'), args[0][0] === '@')
return {
subject: 'npm',
status: `v${version}`,
color: version.split('.')[0] === '0' ? 'orange' : 'blue'
}
}
async function fetchVersion (pkg, scoped) {
// Due to an bug of npm registry api, scoped package need to be handled
// separately: https://github.com/npm/registry/issues/34
if (scoped) {
const endpoint = `https://registry.npmjs.org/${pkg}`
const fullMeta = (await r2(endpoint).json)
return fullMeta.versions[fullMeta['dist-tags']['latest']].version
} else {
2018-07-03 14:45:20 +00:00
// a smaller response for just latest version
2018-06-30 17:52:50 +00:00
const endpointLatest = `https://registry.npmjs.org/${pkg}/latest`
return (await r2(endpointLatest).json).version
}
}