update-icons-version.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import cp from 'child_process'
  2. import fs from 'fs'
  3. import path from 'path'
  4. import { getArgvs, getPackageJson, ICONS_SRC_DIR } from './helpers.mjs'
  5. const p = getPackageJson()
  6. const argv = getArgvs(),
  7. version = argv['latest-version'] || `${p.version}`,
  8. newVersion = argv['new-version'] || `${p.version}`
  9. const setVersions = function(version, files) {
  10. for (const i in files) {
  11. const file = files[i],
  12. filePath = path.join(ICONS_SRC_DIR, `${file}.svg`)
  13. if (fs.existsSync(filePath)) {
  14. let svgFile = fs.readFileSync(filePath).toString()
  15. if (!svgFile.match(/version: ([0-9.]+)/i)) {
  16. svgFile = svgFile.replace(/---\n<svg>/i, function(m) {
  17. return `version: "${version}"\n${m}`
  18. })
  19. fs.writeFileSync(filePath, svgFile)
  20. } else {
  21. console.log(`File ${file} already has version`)
  22. }
  23. } else {
  24. console.log(`File ${file} doesn't exists`)
  25. }
  26. }
  27. }
  28. if (version) {
  29. cp.exec(`grep -RiL "version: " ${ICONS_SRC_DIR}/*.svg`, function(err, ret) {
  30. let newIcons = []
  31. ret.replace(/src\/_icons\/([a-z0-9-]+)\.svg/g, function(m, fileName) {
  32. newIcons.push(fileName)
  33. })
  34. if (newIcons.length) {
  35. setVersions(newVersion.replace(/\.0$/, ''), newIcons)
  36. }
  37. })
  38. }