|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- const fs = require('fs')
- const jsdom = require('jsdom')
- const axios = require('axios')
-
- const fetchBrands = () => new Promise(async (resolve, reject) => {
- try {
- const res = await axios({
- method: 'GET',
- url: process.env.SIMPLEICONS_URL,
- responseType: 'buffer',
- })
-
- const response = res.data.toString('utf-8')
- const dom = new jsdom.JSDOM(response)
- const { window, } = dom
- const linkElements = Array.from(window.document.getElementsByClassName('grid-item__link'))
- const links = linkElements
- .map(el => el.parentElement)
- .map(el => {
- const titleEl = el.getElementsByClassName('grid-item__title')[0]
- const subtitleEl = el.getElementsByClassName('grid-item__subtitle')[0]
-
- return {
- name: titleEl.textContent,
- color: subtitleEl.textContent,
- }
- })
- .sort((a, b) => a.name.localeCompare(b.name))
- const outputStream = fs.createWriteStream(process.env.BRANDS_JSON_FILE_PATH)
- outputStream.on('finish', () => {
- resolve(links)
- })
- outputStream.write(JSON.stringify(links))
- } catch (err) {
- reject(err)
- }
- })
-
- module.exports = () => new Promise((resolve, reject) => {
- fs.readFile(process.env.BRANDS_JSON_FILE_PATH, async (err, file) => {
- if (err.code === 'ENOENT') {
- const result = await fetchBrands()
- resolve(result)
- return
- }
- if (err) {
- reject(err)
- return
- }
- resolve(JSON.parse(file.toString('utf-8')))
- })
- })
|