import.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(/clip-rule="evenodd"/g, '')
  32. .replace(/xmlns="http:\/\/www.w3.org\/2000\/svg"/g, '')
  33. .replace(/<path d="M0 0h24v24H0z"\/>"/g, '')
  34. .replace(/<path stroke="red" stroke-width=".1" d="[^"]+"\s?\/>/g, '')
  35. .replace(/<path[^>]*stroke="red"[^>]*\/>/gs, '')
  36. .replace(/<circle[^>]*stroke="red"[^>]*\/>/gs, '')
  37. .replace(/<g[^>]*stroke="red"[^>]*>.*?<\/g>/gs, '')
  38. .replace(/<svg\s+>/gs, '<svg>')
  39. fileData = optimizeSVG(fileData)
  40. if (filename.match(/\-filled$/)) {
  41. fileData = fileData
  42. .replace(/\/>/g, ' stroke-width="0" fill="currentColor" />')
  43. }
  44. fileData = fileData.replace(/<svg>/g, '---\n---\n<svg>')
  45. if (fs.existsSync(`./src/_icons/${filename}.svg`)) {
  46. const newFileData = fs.readFileSync(`./src/_icons/${filename}.svg`).toString()
  47. const m = newFileData.match(/(---.*---)/gms)
  48. if (m) {
  49. fileData = fileData.replace('---\n---', m[0])
  50. }
  51. } else if (filename.match(/\-filled$/)) {
  52. fileData = fileData
  53. .replace(/---\n---/g, '---\ncategory: Filled\n---')
  54. }
  55. fs.writeFileSync(`./src/_icons/${filename}.svg`, fileData)
  56. })