update-icons.mjs 1.2 KB

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