2018-06-30 17:52:50 +00:00
|
|
|
const r2 = require('r2')
|
2018-07-10 08:17:58 +00:00
|
|
|
const millify = require('millify')
|
2018-06-30 17:52:50 +00:00
|
|
|
|
2018-07-10 03:09:38 +00:00
|
|
|
module.exports = async function (method, ...args) {
|
|
|
|
switch (method) {
|
|
|
|
case 'v':
|
|
|
|
return v(args)
|
2018-07-10 08:17:58 +00:00
|
|
|
case 'dd':
|
|
|
|
return d('last-day', args)
|
|
|
|
case 'dw':
|
|
|
|
return d('last-week', args)
|
|
|
|
case 'dm':
|
|
|
|
return d('last-month', args)
|
2018-07-10 03:09:38 +00:00
|
|
|
default:
|
|
|
|
return {
|
|
|
|
subject: 'npm',
|
|
|
|
status: 'unknown',
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:53:34 +00:00
|
|
|
// npm download
|
|
|
|
async function d (period, args) {
|
|
|
|
const endpoint = `https://api.npmjs.org/downloads/point/${period}/${args.join('/')}`
|
|
|
|
const counts = await r2(endpoint).json
|
|
|
|
return {
|
|
|
|
subject: 'downloads',
|
|
|
|
status: millify(counts.downloads) + period.replace('last-', '%2F'),
|
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:17:58 +00:00
|
|
|
// npm version
|
2018-07-10 03:09:38 +00:00
|
|
|
async function v (args) {
|
2018-07-10 08:53:34 +00:00
|
|
|
const version = await fetchLatestVersion(args)
|
2018-06-30 17:52:50 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
subject: 'npm',
|
|
|
|
status: `v${version}`,
|
|
|
|
color: version.split('.')[0] === '0' ? 'orange' : 'blue'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:53:34 +00:00
|
|
|
async function fetchLatestVersion (args) {
|
|
|
|
const scoped = args.length === 2 && args[0][0] === '@'
|
|
|
|
let endpoint
|
2018-06-30 17:52:50 +00:00
|
|
|
// Due to an bug of npm registry api, scoped package need to be handled
|
|
|
|
// separately: https://github.com/npm/registry/issues/34
|
2018-07-10 08:53:34 +00:00
|
|
|
// A workaround is using version range("*" for "latest") by Andrew Goode:
|
|
|
|
// https://github.com/npm/registry/issues/34#issuecomment-228349870
|
2018-06-30 17:52:50 +00:00
|
|
|
if (scoped) {
|
2018-07-10 08:53:34 +00:00
|
|
|
endpoint = `https://registry.npmjs.org/${args.join('%2F')}/*`
|
2018-06-30 17:52:50 +00:00
|
|
|
} else {
|
2018-07-10 08:53:34 +00:00
|
|
|
endpoint = `https://registry.npmjs.org/${args}/latest`
|
2018-07-10 08:17:58 +00:00
|
|
|
}
|
2018-07-10 08:53:34 +00:00
|
|
|
return (await r2(endpoint).json).version
|
2018-07-10 08:17:58 +00:00
|
|
|
}
|