CognxSafeTrack
Refactor: Technical Debt Repayment (Clean Dashboard, Strict Typing, Pino Logging, SQL Migration)
de6a95b | import * as fs from 'fs'; | |
| import * as path from 'path'; | |
| function processDirectory(srcDir: string, currentDir: string) { | |
| fs.readdirSync(currentDir).forEach(file => { | |
| const fullPath = path.join(currentDir, file); | |
| if (fs.statSync(fullPath).isDirectory()) { | |
| // Ignore dist, node_modules | |
| if (!['node_modules', 'dist', 'scripts'].includes(file)) { | |
| processDirectory(srcDir, fullPath); | |
| } | |
| } else if (fullPath.endsWith('.ts') && fullPath !== path.join(srcDir, 'logger.ts')) { | |
| processFile(srcDir, fullPath); | |
| } | |
| }); | |
| } | |
| function processFile(srcDir: string, filePath: string) { | |
| let content = fs.readFileSync(filePath, 'utf-8'); | |
| const hasLog = content.includes('console.log') || content.includes('console.error') || content.includes('console.warn'); | |
| if (!hasLog) return; | |
| // Replace logs | |
| content = content.replace(/console\.log/g, 'logger.info'); | |
| content = content.replace(/console\.error/g, 'logger.error'); | |
| content = content.replace(/console\.warn/g, 'logger.warn'); | |
| // Calculate relative path for import | |
| const relPath = path.relative(path.dirname(filePath), path.join(srcDir, 'logger')); | |
| let importPath = relPath.startsWith('.') ? relPath : './' + relPath; | |
| // Clean backslash for Windows theoretically, though we are on Mac | |
| importPath = importPath.replace(/\\/g, '/'); | |
| const importStmt = `import { logger } from '${importPath}';\n`; | |
| // Add import statement at the top if not present | |
| if (!content.includes(importStmt)) { | |
| content = importStmt + content; | |
| } | |
| fs.writeFileSync(filePath, content, 'utf-8'); | |
| console.log(`Pino added: ${filePath}`); | |
| } | |
| const targetDirs = [ | |
| path.resolve(__dirname, '..'), // /Volumes/sms/edtech/apps/api/src | |
| path.resolve(__dirname, '../../../whatsapp-worker/src') | |
| ]; | |
| targetDirs.forEach(dir => processDirectory(dir, dir)); | |
| console.log('Logger injection completed!'); | |