Proxy badge lookup for shields.io
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

loadBrands.js 1.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const fs = require('fs')
  2. const jsdom = require('jsdom')
  3. const axios = require('axios')
  4. const fetchBrands = () => new Promise(async (resolve, reject) => {
  5. try {
  6. const res = await axios({
  7. method: 'GET',
  8. url: process.env.SIMPLEICONS_URL,
  9. responseType: 'buffer',
  10. })
  11. const response = res.data.toString('utf-8')
  12. const dom = new jsdom.JSDOM(response)
  13. const { window, } = dom
  14. const linkElements = Array.from(window.document.getElementsByClassName('grid-item__link'))
  15. const links = linkElements
  16. .map(el => el.parentElement)
  17. .map(el => {
  18. const titleEl = el.getElementsByClassName('grid-item__title')[0]
  19. const subtitleEl = el.getElementsByClassName('grid-item__subtitle')[0]
  20. return {
  21. name: titleEl.textContent,
  22. color: subtitleEl.textContent,
  23. }
  24. })
  25. .sort((a, b) => a.name.localeCompare(b.name))
  26. const outputStream = fs.createWriteStream(process.env.BRANDS_JSON_FILE_PATH)
  27. outputStream.on('finish', () => {
  28. resolve(links)
  29. })
  30. outputStream.write(JSON.stringify(links))
  31. } catch (err) {
  32. reject(err)
  33. }
  34. })
  35. module.exports = () => new Promise((resolve, reject) => {
  36. fs.readFile(process.env.BRANDS_JSON_FILE_PATH, async (err, file) => {
  37. if (err.code === 'ENOENT') {
  38. const result = await fetchBrands()
  39. resolve(result)
  40. return
  41. }
  42. if (err) {
  43. reject(err)
  44. return
  45. }
  46. resolve(JSON.parse(file.toString('utf-8')))
  47. })
  48. })