update-icons-unicode.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import glob from 'glob'
  2. import fs from 'fs'
  3. import path from 'path'
  4. import { ICONS_SRC_DIR } from './helpers.mjs'
  5. const getMaxUnicode = () => {
  6. const files = glob.sync(path.join(ICONS_SRC_DIR, '*.svg'))
  7. let maxUnicode = 0
  8. files.forEach(function(file) {
  9. const svgFile = fs.readFileSync(file).toString()
  10. svgFile.replace(/unicode: "([a-f0-9.]+)"/i, function(m, unicode) {
  11. const newUnicode = parseInt(unicode, 16)
  12. if (newUnicode) {
  13. maxUnicode = Math.max(maxUnicode, newUnicode)
  14. }
  15. })
  16. })
  17. return maxUnicode
  18. }
  19. let maxUnicode = getMaxUnicode()
  20. glob(path.join(ICONS_SRC_DIR, '*.svg'), {}, function(er, files) {
  21. for (const i in files) {
  22. const file = files[i]
  23. let svgFile = fs.readFileSync(file).toString()
  24. if (!svgFile.match(/\nunicode: "?([a-f0-9.]+)"?/i)) {
  25. maxUnicode++
  26. const unicode = maxUnicode.toString(16)
  27. if (unicode) {
  28. svgFile = svgFile.replace(/---\n<svg>/i, function(m) {
  29. return `unicode: "${unicode}"\n${m}`
  30. })
  31. console.log(`Add unicode "${unicode}" to "${file}"`)
  32. fs.writeFileSync(file, svgFile)
  33. }
  34. }
  35. }
  36. })