convert-svgs.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. # SVG文件目录
  3. SVG_DIR="public/supply-chain"
  4. # 输出目录
  5. OUTPUT_DIR="src/client/home/pages/SupplyChainDashboards/components/icons"
  6. # 需要转换的SVG文件列表
  7. SVG_FILES=(
  8. "union.svg"
  9. "img5.svg"
  10. "SupplyChainIcons.svg"
  11. "BasePointIcon.svg"
  12. )
  13. # 确保输出目录存在
  14. mkdir -p "$OUTPUT_DIR"
  15. # 转换每个SVG文件
  16. for svg_file in "${SVG_FILES[@]}"; do
  17. svg_path="$SVG_DIR/$svg_file"
  18. # 检查SVG文件是否存在
  19. if [ ! -f "$svg_path" ]; then
  20. echo "警告: SVG文件不存在: $svg_file"
  21. continue
  22. fi
  23. # 生成组件名称
  24. component_name=$(echo "$svg_file" | sed 's/\.svg$//' | sed 's/[_-]\([a-z]\)/\U\1/g' | sed 's/^\([a-z]\)/\U\1/')
  25. # 转换SVG为JSX
  26. jsx_content=$(npx svg-to-jsx "$svg_path")
  27. # 生成React组件代码
  28. component_code="import React from 'react';
  29. interface ${component_name}Props {
  30. className?: string;
  31. fill?: string;
  32. stroke?: string;
  33. width?: number | string;
  34. height?: number | string;
  35. }
  36. const ${component_name}: React.FC<${component_name}Props> = ({
  37. className = \"\",
  38. fill = \"currentColor\",
  39. stroke = \"currentColor\",
  40. width,
  41. height
  42. }) => {
  43. return (
  44. ${jsx_content}
  45. );
  46. };
  47. export default ${component_name};"
  48. # 写入输出文件
  49. output_path="$OUTPUT_DIR/${component_name}.tsx"
  50. echo "$component_code" > "$output_path"
  51. echo "✓ 成功转换: $svg_file -> ${component_name}.tsx"
  52. done
  53. echo "\nSVG转JSX转换完成!"