| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- const fs = require('fs');
- const path = require('path');
- // 目标目录
- const targetDir = path.join(__dirname, '../src/assets/Icons/base/module-process');
- // 递归获取所有 SVG 文件
- function getAllSvgFiles(dir) {
- const files = [];
-
- try {
- const items = fs.readdirSync(dir);
-
- for (const item of items) {
- const fullPath = path.join(dir, item);
- const stat = fs.statSync(fullPath);
-
- if (stat.isDirectory()) {
- files.push(...getAllSvgFiles(fullPath));
- } else if (path.extname(item) === '.svg') {
- files.push(fullPath);
- }
- }
- } catch (error) {
- console.error(`读取目录失败 ${dir}:`, error.message);
- }
-
- return files;
- }
- // 替换文件中的 fill 和 stroke 属性
- function replaceFillAndStrokeInFile(filePath) {
- try {
- // 使用 UTF-8 编码读取文件
- const content = fs.readFileSync(filePath, 'utf8');
-
- // 替换 fill="#000000" 为 fill="currentColor"
- let newContent = content.replace(/fill="#000000"/g, 'fill="currentColor"');
- // 替换 stroke="#000000" 为 stroke="currentColor"
- newContent = newContent.replace(/stroke="#000000"/g, 'stroke="currentColor"');
-
- // 检查是否有变化
- if (content !== newContent) {
- // 使用 UTF-8 编码写入文件
- fs.writeFileSync(filePath, newContent, 'utf8');
- return true; // 表示文件被修改
- }
-
- return false; // 表示文件未修改
- } catch (error) {
- console.error(`处理文件失败 ${filePath}:`, error.message);
- return false;
- }
- }
- // 主函数
- function main() {
- console.log('开始处理 SVG 文件...\n');
- console.log(`目标目录: ${targetDir}\n`);
-
- // 获取所有 SVG 文件
- const svgFiles = getAllSvgFiles(targetDir);
- console.log(`找到 ${svgFiles.length} 个 SVG 文件\n`);
-
- if (svgFiles.length === 0) {
- console.log('没有找到 SVG 文件');
- return;
- }
-
- // 处理每个文件
- let modifiedCount = 0;
-
- for (const filePath of svgFiles) {
- const relativePath = path.relative(targetDir, filePath);
- const wasModified = replaceFillAndStrokeInFile(filePath);
-
- if (wasModified) {
- console.log(`✓ 已修改: ${relativePath}`);
- modifiedCount++;
- } else {
- console.log(`- 跳过: ${relativePath} (未找到 fill="#000000" 或 stroke="#000000")`);
- }
- }
-
- // 输出总结
- console.log('\n处理完成!');
- console.log(`总文件数: ${svgFiles.length}`);
- console.log(`已修改: ${modifiedCount}`);
- console.log(`未修改: ${svgFiles.length - modifiedCount}`);
- }
- // 运行主函数
- main();
|