1
0

fetchTokens.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* eslint-disable no-console */
  2. /* eslint-disable @typescript-eslint/no-var-requires */
  3. const axios = require('axios')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const TOKENS_DIR = path.join(__dirname, '..', 'src/styles/tokens')
  7. const fetchTokenUrlsFromGithub = async () => {
  8. try {
  9. const res = await axios.get('https://api.github.com/repos/Joystream/atlas-resources/contents/design_tokens')
  10. return res.data.filter((file) => file.name.includes('token.json')).map((file) => file.download_url)
  11. } catch (error) {
  12. console.error('Something went wrong. Status code:', error.response.status)
  13. }
  14. }
  15. const TOKEN_WHITE_LIST = ['colors', 'animation', 'typography', 'effects', 'radii']
  16. const fetchTokens = async () => {
  17. fs.mkdir(TOKENS_DIR, { recursive: true }, (err) => {
  18. if (err) {
  19. console.log("Couldn't generate directory", err)
  20. }
  21. })
  22. try {
  23. const tokenUrls = await fetchTokenUrlsFromGithub()
  24. const responses = await axios.all(tokenUrls.map((res) => axios.get(res)))
  25. responses.forEach((res) => {
  26. const fileName = path.basename(res.config.url)
  27. if (!TOKEN_WHITE_LIST.includes(fileName.split('.')[0])) {
  28. return
  29. }
  30. const filePath = path.join(TOKENS_DIR, fileName)
  31. fs.writeFile(filePath, JSON.stringify(res.data, null, 2) + '\n', (err) => {
  32. console.log(`Generating ${fileName} done`)
  33. if (err) {
  34. console.log("Couldn't generate tokens", err)
  35. }
  36. })
  37. })
  38. } catch (error) {
  39. console.error('Something went wrong. Status code:', error.response.status)
  40. }
  41. }
  42. fetchTokens()