update-icons.mjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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(/-->\s?\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. console.log(svgFile);
  29. svgFile = svgFile.replace(/-->\s?\n<svg/i, function (m) {
  30. return `version: "${minorVersion}"\n${m}`
  31. })
  32. console.log(svgFile);
  33. console.log(`Add version "${minorVersion}" to "${file}"`)
  34. fs.writeFileSync(file, svgFile, 'utf8')
  35. }
  36. }
  37. })