2
0

convert-svgs.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "img7.svg"
  13. "SupplyChainNetwork.svg"
  14. "PopupInfoBox.svg"
  15. )
  16. # 确保输出目录存在
  17. mkdir -p "$OUTPUT_DIR"
  18. # 转换每个SVG文件
  19. for svg_file in "${SVG_FILES[@]}"; do
  20. svg_path="$SVG_DIR/$svg_file"
  21. # 检查SVG文件是否存在
  22. if [ ! -f "$svg_path" ]; then
  23. echo "警告: SVG文件不存在: $svg_file"
  24. continue
  25. fi
  26. # 生成组件名称
  27. component_name=$(echo "$svg_file" | sed 's/\.svg$//' | sed 's/[_-]\([a-z]\)/\U\1/g' | sed 's/^\([a-z]\)/\U\1/')
  28. # 转换SVG为JSX
  29. jsx_content=$(npx svg-to-jsx "$svg_path")
  30. # 生成React组件代码
  31. component_code="import React from 'react';
  32. interface ${component_name}Props {
  33. className?: string;
  34. fill?: string;
  35. stroke?: string;
  36. width?: number | string;
  37. height?: number | string;
  38. }
  39. const ${component_name}: React.FC<${component_name}Props> = ({
  40. className = \"\",
  41. fill = \"currentColor\",
  42. stroke = \"currentColor\",
  43. width,
  44. height
  45. }) => {
  46. return (
  47. ${jsx_content}
  48. );
  49. };
  50. export default ${component_name};"
  51. # 写入输出文件
  52. output_path="$OUTPUT_DIR/${component_name}.tsx"
  53. echo "$component_code" > "$output_path"
  54. echo "✓ 成功转换: $svg_file -> ${component_name}.tsx"
  55. done
  56. echo "\nSVG转JSX转换完成!"