Browse Source

Use dynamic URL

Remove dependency with Axios.
master
TheoryOfNekomata 4 years ago
parent
commit
db8fb3547d
2 changed files with 10 additions and 22 deletions
  1. +9
    -22
      getBadgeUrl.js
  2. +1
    -0
      index.js

+ 9
- 22
getBadgeUrl.js View File

@@ -1,6 +1,3 @@
const axios = require('axios')
const path = require('path')

const RESOLVE = require('./resolve.json') const RESOLVE = require('./resolve.json')
const loadBrands = require('./loadBrands') const loadBrands = require('./loadBrands')
const getLibraryColor = require('./getLibraryColor') const getLibraryColor = require('./getLibraryColor')
@@ -9,48 +6,38 @@ module.exports = async ({
type, type,
repo, repo,
dependency: library, dependency: library,
kind,
color, color,
}) => { }) => {
if (!repo) { if (!repo) {
return null return null
} }

const labelColor = '666666' const labelColor = '666666'
const logoColor = 'ffffff' const logoColor = 'ffffff'
const style = 'flat-square' const style = 'flat-square'


const { data, } = await axios({
method: 'GET',
url: path.join(process.env.REPO_BASE_URL, repo, 'raw', 'branch', process.env.MAIN_BRANCH, 'package.json')
})

const { name, version, dependencies, devDependencies, } = data

let badgeUrl let badgeUrl


switch (type) { switch (type) {
case 'version': case 'version':
badgeUrl = new URL(`/badge/${name.replace(/-/g, '--')}-${version}-${color}`, process.env.SHIELDS_IMAGE_BASE_URL)
badgeUrl = new URL(`/badge/dynamic/json`, process.env.SHIELDS_IMAGE_BASE_URL)
badgeUrl.search = new URLSearchParams({ badgeUrl.search = new URLSearchParams({
color,
query: '$.version',
label: repo,
style, style,
labelColor, labelColor,
}).toString() }).toString()
return badgeUrl return badgeUrl
case 'dependency': case 'dependency':
let libraryVersion
if (Object.keys(dependencies).includes(library)) {
libraryVersion = dependencies[library]
} else if (Object.keys(devDependencies).includes(library)) {
libraryVersion = devDependencies[library]
} else {
libraryVersion = '0.0.0'
}

const brands = await loadBrands() const brands = await loadBrands()
const libraryInfo = getLibraryColor(library, brands, RESOLVE) const libraryInfo = getLibraryColor(library, brands, RESOLVE)
const libraryColor = libraryInfo ? libraryInfo.color.replace('#', '') : `${labelColor}` const libraryColor = libraryInfo ? libraryInfo.color.replace('#', '') : `${labelColor}`
badgeUrl = new URL(`/badge/${library.replace(/-/g, '--')}-${libraryVersion}-${libraryColor}`, process.env.SHIELDS_IMAGE_BASE_URL)
badgeUrl = new URL(`/badge/dynamic/json`, process.env.SHIELDS_IMAGE_BASE_URL)
badgeUrl.search = new URLSearchParams({ badgeUrl.search = new URLSearchParams({
color: libraryColor,
label: library,
query: `$.${kind}['${library}']`,
labelColor, labelColor,
logo: libraryInfo.name, logo: libraryInfo.name,
logoColor, logoColor,


+ 1
- 0
index.js View File

@@ -17,6 +17,7 @@ const server = http.createServer(async (req, res) => {
}), }),
{} {}
) )

const badgeUrl = await getBadgeUrl(searchParams) const badgeUrl = await getBadgeUrl(searchParams)


if (badgeUrl === null) { if (badgeUrl === null) {


Loading…
Cancel
Save