-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-logging.js
More file actions
88 lines (71 loc) · 3.15 KB
/
fix-logging.js
File metadata and controls
88 lines (71 loc) · 3.15 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require('fs');
const path = require('path');
// Get all TypeScript files in src
function getAllTsFiles(dir) {
const files = [];
const entries = fs.readdirSync(dir);
for (const entry of entries) {
const fullPath = path.join(dir, entry);
const stat = fs.statSync(fullPath);
if (stat.isDirectory() && !entry.startsWith('.') && !entry.includes('__tests__')) {
files.push(...getAllTsFiles(fullPath));
} else if ((entry.endsWith('.ts') || entry.endsWith('.tsx')) && !entry.includes('.test.')) {
files.push(fullPath);
}
}
return files;
}
const files = getAllTsFiles('./src');
let totalReplacements = 0;
for (const filePath of files) {
let content = fs.readFileSync(filePath, 'utf8');
let changed = false;
// Check if file contains console logging
if (/console\.(log|error|warn|info)/.test(content)) {
// Add pino import if needed
if (!content.includes('from \'pino\'') && !content.includes('pino')) {
// Add pino import
const importLine = `import { pino } from 'pino'\n`;
// Add logger constant
const loggerLine = `const logger = pino({ name: '${path.basename(filePath, path.extname(filePath))}' })\n\n`;
// Find the first non-import line to insert logger
const lines = content.split('\n');
let insertIndex = 0;
// Find last import line
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('import ') || lines[i].startsWith('export ') && lines[i].includes('import')) {
insertIndex = i + 1;
} else if (lines[i].trim() === '' || lines[i].startsWith('//') || lines[i].startsWith('/*')) {
// Skip empty lines and comments
continue;
} else {
break;
}
}
lines.splice(insertIndex, 0, importLine, loggerLine);
content = lines.join('\n');
changed = true;
}
// Replace console.log with logger.info
const beforeLog = (content.match(/console\.log\(/g) || []).length;
content = content.replace(/console\.log\(/g, 'logger.info(');
totalReplacements += beforeLog - (content.match(/console\.log\(/g) || []).length;
// Replace console.error with logger.error
const beforeError = (content.match(/console\.error\(/g) || []).length;
content = content.replace(/console\.error\(/g, 'logger.error(');
totalReplacements += beforeError - (content.match(/console\.error\(/g) || []).length;
// Replace console.warn with logger.warn
const beforeWarn = (content.match(/console\.warn\(/g) || []).length;
content = content.replace(/console\.warn\(/g, 'logger.warn(');
totalReplacements += beforeWarn - (content.match(/console\.warn\(/g) || []).length;
// Replace console.info with logger.info
const beforeInfo = (content.match(/console\.info\(/g) || []).length;
content = content.replace(/console\.info\(/g, 'logger.info(');
totalReplacements += beforeInfo - (content.match(/console\.info\(/g) || []).length;
if (changed || beforeLog + beforeError + beforeWarn + beforeInfo > 0) {
fs.writeFileSync(filePath, content);
console.log(`Updated ${filePath}`);
}
}
}
console.log(`Total console.* replacements: ${totalReplacements}`);