import.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import fs from 'fs'
  2. import { glob } from 'glob'
  3. import { resolve, basename } from 'path'
  4. import { HOME_DIR, optimizeSVG, iconTemplate, types } from './helpers.mjs'
  5. types.forEach(type => {
  6. const files = glob.sync(resolve(HOME_DIR, `./new/${type}/*.svg`))
  7. files.forEach(function (file, i) {
  8. let fileData = fs.readFileSync(file).toString(),
  9. filename = basename(file, '.svg')
  10. console.log(`${type}/${filename}`)
  11. fileData = optimizeSVG(fileData)
  12. if (fileData.match(/transform="/)) {
  13. throw new Error(`File ${file} has \`transform\` in code!!`)
  14. }
  15. if (filename.match(/\s/)) {
  16. throw new Error(`File ${file} has space in name!!`)
  17. }
  18. fileData = fileData.replace(/---/g, '')
  19. .replace(/fill="none"/g, '')
  20. .replace(/fill="#D8D8D8"/gi, '')
  21. .replace(/fill-rule="evenodd"/g, '')
  22. .replace(/stroke-linecap="round"/g, '')
  23. .replace(/stroke-linejoin="round"/g, '')
  24. .replace(/viewBox="0 0 24 24"/g, '')
  25. .replace(/stroke="#000000"/g, '')
  26. .replace(/stroke="#000"/g, '')
  27. .replace(/stroke-width="2"/g, '')
  28. .replace(/width="24"/g, '')
  29. .replace(/width="24px"/g, '')
  30. .replace(/height="24"/g, '')
  31. .replace(/height="24px"/g, '')
  32. .replace(/clip-rule="evenodd"/g, '')
  33. .replace(/xmlns="http:\/\/www.w3.org\/2000\/svg"/g, '')
  34. .replace(/<path d="M0 0h24v24H0z"\/>"/g, '')
  35. .replace(/<path stroke="red" stroke-width=".1" d="[^"]+"\s?\/>/g, '')
  36. .replace(/<path[^>]*fill-opacity=".1"[^>]*\/>/g, '')
  37. .replace(/<path[^>]*stroke="red"[^>]*\/>/gs, '')
  38. .replace(/<circle[^>]*stroke="red"[^>]*\/>/gs, '')
  39. .replace(/<path[^>]*fill="red"[^>]*\/>/gs, '')
  40. .replace(/<g[^>]*stroke="red"[^>]*>.*?<\/g>/gs, '')
  41. .replace(/<svg\s+>/gs, '<svg>')
  42. fileData = optimizeSVG(fileData)
  43. fileData = fileData.replace(/<svg>/g, `<!--\n-->\n${iconTemplate(type)}`)
  44. if (type == "filled") {
  45. fileData = fileData
  46. .replace('stroke-width="2"', '')
  47. .replace('stroke-linecap="round"', '')
  48. .replace('stroke-linejoin="round"', '')
  49. .replace('stroke="currentColor"', '')
  50. .replace('fill="none"', 'fill="currentColor"')
  51. // remove empty lines
  52. .replace(/^\s*[\r\n]/gm, '')
  53. }
  54. if (fs.existsSync(`./icons/${type}/${filename}.svg`)) {
  55. const newFileData = fs.readFileSync(`./icons/${type}/${filename}.svg`).toString()
  56. const m = newFileData.match(/(<!--.*-->)/gms)
  57. if (m) {
  58. fileData = fileData.replace('<!--\n-->', m[0])
  59. }
  60. } else if (filename.match(/\-filled$/)) {
  61. fileData = fileData
  62. .replace(/<!--\n-->/g, '<!--\ncategory: Filled\n-->')
  63. } else if (filename.match(/brand\-/)) {
  64. fileData = fileData
  65. .replace(/<!--\n-->/g, '<!--\ncategory: Brand\n-->')
  66. }
  67. fs.writeFileSync(`./icons/${type}/${filename}.svg`, fileData)
  68. })
  69. })