Proxy badge lookup for shields.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

  1. const axios = require('axios')
  2. const path = require('path')
  3. const RESOLVE = require('./resolve.json')
  4. const loadBrands = require('./loadBrands')
  5. const getLibraryColor = require('./getLibraryColor')
  6. module.exports = async ({
  7. type,
  8. repo,
  9. dependency: library,
  10. color,
  11. }) => {
  12. if (!repo) {
  13. return null
  14. }
  15. const labelColor = '666666'
  16. const logoColor = 'ffffff'
  17. const style = 'flat-square'
  18. const { data, } = await axios({
  19. method: 'GET',
  20. url: path.join(process.env.REPO_BASE_URL, repo, 'raw', 'branch', process.env.MAIN_BRANCH, 'package.json')
  21. })
  22. const { name, version, dependencies, devDependencies, } = data
  23. let badgeUrl
  24. switch (type) {
  25. case 'version':
  26. badgeUrl = new URL(`/badge/${name.replace(/-/g, '--')}-${version}-${color}`, process.env.SHIELDS_IMAGE_BASE_URL)
  27. badgeUrl.search = new URLSearchParams({
  28. style,
  29. labelColor,
  30. }).toString()
  31. return badgeUrl
  32. case 'dependency':
  33. let libraryVersion
  34. if (Object.keys(dependencies).includes(library)) {
  35. libraryVersion = dependencies[library]
  36. } else if (Object.keys(devDependencies).includes(library)) {
  37. libraryVersion = devDependencies[library]
  38. } else {
  39. libraryVersion = '0.0.0'
  40. }
  41. const brands = await loadBrands()
  42. const libraryInfo = getLibraryColor(library, brands, RESOLVE)
  43. const libraryColor = libraryInfo ? libraryInfo.color.replace('#', '') : `${labelColor}`
  44. badgeUrl = new URL(`/badge/${library.replace(/-/g, '--')}-${libraryVersion}-${libraryColor}`, process.env.SHIELDS_IMAGE_BASE_URL)
  45. badgeUrl.search = new URLSearchParams({
  46. labelColor,
  47. logo: libraryInfo.name,
  48. logoColor,
  49. style,
  50. })
  51. return badgeUrl
  52. default:
  53. break
  54. }
  55. return null
  56. }