From 30e35d702e5e2c6784a23641317e2cb7ac9f2146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Tue, 29 Dec 2020 12:21:53 +0100 Subject: [PATCH] badge(shards): add Crystal shards service (#468) * feat: add crystal shards service * badge(shards): remove duplicated version example Co-authored-by: Amio Jin --- api/shards.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++++ libs/badge-list.ts | 1 + 2 files changed, 60 insertions(+) create mode 100644 api/shards.ts diff --git a/api/shards.ts b/api/shards.ts new file mode 100644 index 0000000..3d83ae4 --- /dev/null +++ b/api/shards.ts @@ -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/: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*]*?>([^<]+)<\//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' + } + } + } +} diff --git a/libs/badge-list.ts b/libs/badge-list.ts index a81dd97..44eb197 100644 --- a/libs/badge-list.ts +++ b/libs/badge-list.ts @@ -36,6 +36,7 @@ export const liveBadgeList = [ 'winget', 'f-droid', 'pub', + 'shards', 'wapm', // CI 'travis',