badge(shards): add Crystal shards service (#468)

* feat: add crystal shards service

* badge(shards): remove duplicated version example

Co-authored-by: Amio Jin <amio.cn@gmail.com>
pull/473/head
Dario Vladović 2020-12-29 12:21:53 +01:00 zatwierdzone przez GitHub
rodzic e6f9e76d10
commit 30e35d702e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 60 dodań i 0 usunięć

59
api/shards.ts 100644
Wyświetl plik

@ -0,0 +1,59 @@
import got from '../libs/got'
import { millify, version, versionColor } from '../libs/utils'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
const SHARDS_REPO_URL = 'https://shardbox.org/'
const client = got.extend({ prefixUrl: SHARDS_REPO_URL })
export default createBadgenHandler({
title: 'Crystal shards',
examples: {
'/shards/v/kemal': 'version',
'/shards/license/clear': 'license',
'/shards/crystal/amber': 'crystal version',
'/shards/dependents/lucky': 'dependents'
},
handlers: {
'/shards/:topic<v|license|crystal|dependents>/:shard': handler
}
})
async function handler({ topic, shard }: PathArgs) {
const html = await client.get(`shards/${shard}`).text()
switch (topic) {
case 'v':
const ver = html.match(/class="version">([^<]+)<\//i)?.[1].trim()
return {
subject: 'shards',
status: version(ver),
color: versionColor(ver)
}
case 'license': {
const license = html.match(/opensource.org\/licenses\/[^>]+?>([^<]+)<\//i)?.[1].trim()
return {
subject: 'license',
status: license || 'unknown',
color: 'blue'
}
}
case 'crystal': {
let ver = html.match(/Crystal<\/span>\s*<span[^>]*?>([^<]+)<\//i)?.[1].trim()
if (!ver || ver === 'none') ver = '*'
return {
subject: 'crystal',
status: version(ver),
color: 'green'
}
}
case 'dependents': {
const dependents = Number(html.match(/Dependents[^>]*? class="count">([^<]+)<\//i)?.[1].trim())
return {
subject: 'dependents',
status: millify(dependents),
color: 'green'
}
}
}
}

Wyświetl plik

@ -36,6 +36,7 @@ export const liveBadgeList = [
'winget',
'f-droid',
'pub',
'shards',
'wapm',
// CI
'travis',