import.mjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import fs from 'fs'
  2. import glob from 'glob'
  3. import { resolve, basename } from 'path'
  4. import { HOME_DIR, optimizeSVG } from './helpers.mjs'
  5. const files = glob.sync(resolve(HOME_DIR, './new/*.svg'))
  6. files.forEach(function(file, i) {
  7. let fileData = fs.readFileSync(file).toString(),
  8. filename = basename(file, '.svg')
  9. console.log(filename)
  10. fileData = optimizeSVG(fileData)
  11. if (fileData.match(/transform="/)) {
  12. throw new Error(`File ${file} has \`transform\` in code!!`)
  13. }
  14. if (filename.match(/\s/)) {
  15. throw new Error(`File ${file} has space in name!!`)
  16. }
  17. fileData = fileData.replace(/---/g, '')
  18. .replace(/fill="none"/g, '')
  19. .replace(/fill="#D8D8D8"/gi, '')
  20. .replace(/fill-rule="evenodd"/g, '')
  21. .replace(/stroke-linecap="round"/g, '')
  22. .replace(/stroke-linejoin="round"/g, '')
  23. .replace(/viewBox="0 0 24 24"/g, '')
  24. .replace(/stroke="#000000"/g, '')
  25. .replace(/stroke="#000"/g, '')
  26. .replace(/stroke-width="2"/g, '')
  27. .replace(/width="24"/g, '')
  28. .replace(/width="24px"/g, '')
  29. .replace(/height="24"/g, '')
  30. .replace(/height="24px"/g, '')
  31. .replace(/xmlns="http:\/\/www.w3.org\/2000\/svg"/g, '')
  32. .replace(/<path d="M0 0h24v24H0z"\/>"/g, '')
  33. .replace(/<path stroke="red" stroke-width=".1" d="[^"]+"\s?\/>/g, '')
  34. .replace(/<path[^>]*stroke="red"[^>]*\/>/gs, '')
  35. .replace(/<circle[^>]*stroke="red"[^>]*\/>/gs, '')
  36. .replace(/<g[^>]*stroke="red"[^>]*>.*?<\/g>/gs, '')
  37. fileData = optimizeSVG(fileData)
  38. fileData = fileData.replace(/<svg>/g, '---\n---\n<svg>')
  39. if (fs.existsSync(`./src/_icons/${filename}.svg`)) {
  40. const newFileData = fs.readFileSync(`./src/_icons/${filename}.svg`).toString()
  41. const m = newFileData.match(/(---.*---)/gms)
  42. if (m) {
  43. fileData = fileData.replace('---\n---', m[0])
  44. }
  45. }
  46. fs.writeFileSync(`./src/_icons/${filename}.svg`, fileData)
  47. })