script.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const path = require('node:path')
  2. const { open, readdir, access, mkdir, writeFile, appendFile, rm } = require('node:fs/promises')
  3. const { parseXml } = require('@rgrove/parse-xml')
  4. const camelCase = require('lodash/camelCase')
  5. const template = require('lodash/template')
  6. const generateDir = async (currentPath) => {
  7. try {
  8. await mkdir(currentPath, { recursive: true })
  9. }
  10. catch (err) {
  11. console.error(err.message)
  12. }
  13. }
  14. const processSvgStructure = (svgStructure, replaceFillOrStrokeColor) => {
  15. if (svgStructure?.children.length) {
  16. svgStructure.children = svgStructure.children.filter(c => c.type !== 'text')
  17. svgStructure.children.forEach((child) => {
  18. if (child?.name === 'path' && replaceFillOrStrokeColor) {
  19. if (child?.attributes?.stroke)
  20. child.attributes.stroke = 'currentColor'
  21. if (child?.attributes.fill)
  22. child.attributes.fill = 'currentColor'
  23. }
  24. if (child?.children.length)
  25. processSvgStructure(child, replaceFillOrStrokeColor)
  26. })
  27. }
  28. }
  29. const generateSvgComponent = async (fileHandle, entry, pathList, replaceFillOrStrokeColor) => {
  30. const currentPath = path.resolve(__dirname, 'src', ...pathList.slice(2))
  31. try {
  32. await access(currentPath)
  33. }
  34. catch {
  35. await generateDir(currentPath)
  36. }
  37. const svgString = await fileHandle.readFile({ encoding: 'utf8' })
  38. const svgJson = parseXml(svgString).toJSON()
  39. const svgStructure = svgJson.children[0]
  40. processSvgStructure(svgStructure, replaceFillOrStrokeColor)
  41. const prefixFileName = camelCase(entry.split('.')[0])
  42. const fileName = prefixFileName.charAt(0).toUpperCase() + prefixFileName.slice(1)
  43. const svgData = {
  44. icon: svgStructure,
  45. name: fileName,
  46. }
  47. const componentRender = template(`
  48. // GENERATE BY script
  49. // DON NOT EDIT IT MANUALLY
  50. import * as React from 'react'
  51. import data from './<%= svgName %>.json'
  52. import IconBase from '@/app/components/base/icons/IconBase'
  53. import type { IconBaseProps, IconData } from '@/app/components/base/icons/IconBase'
  54. const Icon = React.forwardRef<React.MutableRefObject<SVGElement>, Omit<IconBaseProps, 'data'>>((
  55. props,
  56. ref,
  57. ) => <IconBase {...props} ref={ref} data={data as IconData} />)
  58. export default Icon
  59. `.trim())
  60. await writeFile(path.resolve(currentPath, `${fileName}.json`), JSON.stringify(svgData, '', '\t'))
  61. await writeFile(path.resolve(currentPath, `${fileName}.tsx`), `${componentRender({ svgName: fileName })}\n`)
  62. const indexingRender = template(`
  63. export { default as <%= svgName %> } from './<%= svgName %>'
  64. `.trim())
  65. await appendFile(path.resolve(currentPath, 'index.ts'), `${indexingRender({ svgName: fileName })}\n`)
  66. }
  67. const walk = async (entry, pathList, replaceFillOrStrokeColor) => {
  68. const currentPath = path.resolve(...pathList, entry)
  69. let fileHandle
  70. try {
  71. fileHandle = await open(currentPath)
  72. const stat = await fileHandle.stat()
  73. if (stat.isDirectory()) {
  74. const files = await readdir(currentPath)
  75. for (const file of files)
  76. await walk(file, [...pathList, entry], replaceFillOrStrokeColor)
  77. }
  78. if (stat.isFile() && /.+\.svg$/g.test(entry))
  79. await generateSvgComponent(fileHandle, entry, pathList, replaceFillOrStrokeColor)
  80. }
  81. finally {
  82. fileHandle?.close()
  83. }
  84. }
  85. (async () => {
  86. await rm(path.resolve(__dirname, 'src'), { recursive: true, force: true })
  87. await walk('public', [__dirname, 'assets'])
  88. await walk('vender', [__dirname, 'assets'], true)
  89. })()