File size: 1,984 Bytes
de6a95b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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!');
|