update-icons.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { globSync, glob } from 'glob'
  2. import fs from 'fs'
  3. import path from 'path'
  4. import { ICONS_SRC_DIR, getMaxUnicode, getArgvs, getPackageJson } from './helpers.mjs'
  5. const p = getPackageJson(),
  6. argv = getArgvs(),
  7. newVersion = argv['new-version'] || `${p.version}`
  8. console.log(argv);
  9. const files = globSync(path.join(ICONS_SRC_DIR, '**/*.svg'))
  10. let maxUnicode = getMaxUnicode()
  11. files.forEach(function (file) {
  12. let svgFile = fs.readFileSync(file).toString()
  13. // Add unicode to svg files
  14. if (!svgFile.match(/\nunicode: "?([a-f0-9.]+)"?/i)) {
  15. maxUnicode++
  16. const unicode = maxUnicode.toString(16)
  17. if (unicode) {
  18. svgFile = svgFile.replace(/-->\n<svg/i, function (m) {
  19. return `unicode: "${unicode}"\n${m}`
  20. })
  21. console.log(`Add unicode "${unicode}" to "${file}"`)
  22. fs.writeFileSync(file, svgFile, 'utf8')
  23. }
  24. }
  25. if (newVersion) {
  26. // Add version to svg files
  27. if (!svgFile.match(/\nversion: "?([a-f0-9.]+)"?/i)) {
  28. svgFile = svgFile.replace(/-->\n<svg/i, function (m) {
  29. return `version: "${newVersion}"\n${m}`
  30. })
  31. console.log(`Add version "${newVersion}" to "${file}"`)
  32. fs.writeFileSync(file, svgFile, 'utf8')
  33. }
  34. }
  35. })