const axios = require('axios') const path = require('path') const RESOLVE = require('./resolve.json') const loadBrands = require('./loadBrands') const getLibraryColor = require('./getLibraryColor') module.exports = async ({ type, repo, dependency: library, color, }) => { if (!repo) { return null } const labelColor = '666666' const logoColor = 'ffffff' 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 switch (type) { case 'version': badgeUrl = new URL(`/badge/${name.replace(/-/g, '--')}-${version}-${color}`, process.env.SHIELDS_IMAGE_BASE_URL) badgeUrl.search = new URLSearchParams({ style, labelColor, }).toString() return badgeUrl 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 libraryInfo = getLibraryColor(library, brands, RESOLVE) const libraryColor = libraryInfo ? libraryInfo.color.replace('#', '') : `${labelColor}` badgeUrl = new URL(`/badge/${library.replace(/-/g, '--')}-${libraryVersion}-${libraryColor}`, process.env.SHIELDS_IMAGE_BASE_URL) badgeUrl.search = new URLSearchParams({ labelColor, logo: libraryInfo.name, logoColor, style, }) return badgeUrl default: break } return null }