replace-svg-fill.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const fs = require('fs');
  2. const path = require('path');
  3. // 目标目录
  4. const targetDir = path.join(__dirname, '../src/assets/Icons/base/module-process');
  5. // 递归获取所有 SVG 文件
  6. function getAllSvgFiles(dir) {
  7. const files = [];
  8. try {
  9. const items = fs.readdirSync(dir);
  10. for (const item of items) {
  11. const fullPath = path.join(dir, item);
  12. const stat = fs.statSync(fullPath);
  13. if (stat.isDirectory()) {
  14. files.push(...getAllSvgFiles(fullPath));
  15. } else if (path.extname(item) === '.svg') {
  16. files.push(fullPath);
  17. }
  18. }
  19. } catch (error) {
  20. console.error(`读取目录失败 ${dir}:`, error.message);
  21. }
  22. return files;
  23. }
  24. // 替换文件中的 fill 和 stroke 属性
  25. function replaceFillAndStrokeInFile(filePath) {
  26. try {
  27. // 使用 UTF-8 编码读取文件
  28. const content = fs.readFileSync(filePath, 'utf8');
  29. // 替换 fill="#000000" 为 fill="currentColor"
  30. let newContent = content.replace(/fill="#000000"/g, 'fill="currentColor"');
  31. // 替换 stroke="#000000" 为 stroke="currentColor"
  32. newContent = newContent.replace(/stroke="#000000"/g, 'stroke="currentColor"');
  33. // 检查是否有变化
  34. if (content !== newContent) {
  35. // 使用 UTF-8 编码写入文件
  36. fs.writeFileSync(filePath, newContent, 'utf8');
  37. return true; // 表示文件被修改
  38. }
  39. return false; // 表示文件未修改
  40. } catch (error) {
  41. console.error(`处理文件失败 ${filePath}:`, error.message);
  42. return false;
  43. }
  44. }
  45. // 主函数
  46. function main() {
  47. console.log('开始处理 SVG 文件...\n');
  48. console.log(`目标目录: ${targetDir}\n`);
  49. // 获取所有 SVG 文件
  50. const svgFiles = getAllSvgFiles(targetDir);
  51. console.log(`找到 ${svgFiles.length} 个 SVG 文件\n`);
  52. if (svgFiles.length === 0) {
  53. console.log('没有找到 SVG 文件');
  54. return;
  55. }
  56. // 处理每个文件
  57. let modifiedCount = 0;
  58. for (const filePath of svgFiles) {
  59. const relativePath = path.relative(targetDir, filePath);
  60. const wasModified = replaceFillAndStrokeInFile(filePath);
  61. if (wasModified) {
  62. console.log(`✓ 已修改: ${relativePath}`);
  63. modifiedCount++;
  64. } else {
  65. console.log(`- 跳过: ${relativePath} (未找到 fill="#000000" 或 stroke="#000000")`);
  66. }
  67. }
  68. // 输出总结
  69. console.log('\n处理完成!');
  70. console.log(`总文件数: ${svgFiles.length}`);
  71. console.log(`已修改: ${modifiedCount}`);
  72. console.log(`未修改: ${svgFiles.length - modifiedCount}`);
  73. }
  74. // 运行主函数
  75. main();