Proxy badge lookup for shields.io
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

53 satır
1.5 KiB

  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 && 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. })