| 26 | ]; |
| 27 | |
| 28 | function updateScriptLogging(filePath) { |
| 29 | try { |
| 30 | let content = fs.readFileSync(filePath, 'utf8'); |
| 31 | const originalContent = content; |
| 32 | |
| 33 | // 检查是否已经导入了BuildLogger |
| 34 | const hasLoggerImport = /const\s+(?:BuildLogger|Logger)\s*=\s*require/.test(content); |
| 35 | |
| 36 | // 检查是否有console.log需要替换 |
| 37 | const hasConsoleLog = /console\.log\s*\(/.test(content); |
| 38 | |
| 39 | if (!hasConsoleLog) { |
| 40 | console.log(`⏭️ 跳过 ${path.basename(filePath)} - 无console.log`); |
| 41 | return { replaced: false }; |
| 42 | } |
| 43 | |
| 44 | // 替换console.log为BuildLogger.log |
| 45 | // 识别带emoji的success消息 |
| 46 | content = content.replace(/console\.log\((['"`])✅([^'"`]*)\1\)/g, 'BuildLogger.success(\'$2\')'); |
| 47 | content = content.replace(/console\.log\((['"`])❌([^'"`]*)\1\)/g, 'BuildLogger.error(\'$2\')'); |
| 48 | content = content.replace(/console\.log\((['"`])⚠️([^'"`]*)\1\)/g, 'BuildLogger.warn(\'$2\')'); |
| 49 | content = content.replace(/console\.log\((['"`])🔧([^'"`]*)\1\)/g, 'BuildLogger.log(\'🔧$2\')'); |
| 50 | content = content.replace(/console\.log\((['"`])📊([^'"`]*)\1\)/g, 'BuildLogger.log(\'📊$2\')'); |
| 51 | |
| 52 | // 替换剩余的console.log |
| 53 | content = content.replace(/console\.log\s*\(/g, 'BuildLogger.log('); |
| 54 | |
| 55 | // 如果还没有导入BuildLogger,在文件开头添加导入 |
| 56 | if (!hasLoggerImport && hasConsoleLog) { |
| 57 | // 在第一个require之后或文件开头添加 |
| 58 | const importStatement = `const BuildLogger = require('./logger.js');\n`; |
| 59 | |
| 60 | if (/^const\s+/.test(content)) { |
| 61 | // 在最后一个require之后添加 |
| 62 | const lastRequireMatch = content.match(/const\s+\w+\s*=\s*require\([^)]+\);?/g); |
| 63 | if (lastRequireMatch && lastRequireMatch.length > 0) { |
| 64 | const lastRequire = lastRequireMatch[lastRequireMatch.length - 1]; |
| 65 | const lastRequireIndex = content.lastIndexOf(lastRequire); |
| 66 | const insertIndex = lastRequireIndex + lastRequire.length; |
| 67 | content = content.slice(0, insertIndex) + '\n' + importStatement + content.slice(insertIndex); |
| 68 | } else { |
| 69 | content = importStatement + content; |
| 70 | } |
| 71 | } else { |
| 72 | content = importStatement + '\n' + content; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (content !== originalContent) { |
| 77 | fs.writeFileSync(filePath, content, 'utf8'); |
| 78 | const count = (originalContent.match(/console\.log\s*\(/g) || []).length; |
| 79 | console.log(`✅ ${path.basename(filePath)} - 替换了${count}处console.log`); |
| 80 | return { replaced: true, count }; |
| 81 | } else { |
| 82 | console.log(`⏭️ 跳过 ${path.basename(filePath)} - 无需修改`); |
| 83 | return { replaced: false }; |
| 84 | } |
| 85 | |