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.

62 lines
1.8 KiB

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