Repository navigation
Expand file tree
/
Copy pathreplace_logs.js
More file actions
77 lines (63 loc) · 2.64 KB
/
Copy pathreplace_logs.js
File metadata and controls
77 lines (63 loc) · 2.64 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
const fs = require('fs');
const path = require('path');
const dir = path.join(__dirname, 'App/src');
const sensitiveKeywords = ['pin', 'token', 'secret', 'seed', 'payload', 'tx', 'transaction', 'key', 'auth', 'password', 'mnemonic'];
function processFile(filePath) {
if (!filePath.match(/\.(ts|tsx)$/) || filePath.includes('logger.ts')) return;
let content = fs.readFileSync(filePath, 'utf8');
let originalContent = content;
let hasChanges = false;
let hasLoggerImport = content.includes('import { Logger }');
const lines = content.split('\n');
let newLines = [];
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (line.includes('console.')) {
const lowerLine = line.toLowerCase();
const isSensitive = sensitiveKeywords.some(kw => lowerLine.includes(kw));
if (isSensitive) {
newLines.push('// [SECURITY] Removed sensitive log: ' + line.trim().replace(/\*/g, ''));
hasChanges = true;
continue;
} else {
line = line.replace(/console\.(log|error|warn|info|debug)/g, (match, level) => {
if (level === 'log') return 'Logger.info';
return 'Logger.' + level;
});
hasChanges = true;
}
}
newLines.push(line);
}
if (hasChanges) {
let finalContent = newLines.join('\n');
if (!hasLoggerImport) {
const relativeDepth = filePath.replace(dir, '').split(path.sep).length - 2;
let importPath = '';
if (relativeDepth === 0) importPath = './utils/logger';
else if (relativeDepth === 1) importPath = '../utils/logger';
else if (relativeDepth === 2) importPath = '../../utils/logger';
else if (relativeDepth === 3) importPath = '../../../utils/logger';
if (filePath.includes('utils') && relativeDepth === 1) importPath = './logger';
if (filePath.includes('hooks') && relativeDepth === 1) importPath = '../utils/logger';
if (filePath.includes('services') && relativeDepth === 1) importPath = '../utils/logger';
if (filePath.includes('components') && relativeDepth === 1) importPath = '../utils/logger';
if (filePath.includes('components') && relativeDepth === 2) importPath = '../../utils/logger';
finalContent = "import { Logger } from '" + importPath + "';\n" + finalContent;
}
fs.writeFileSync(filePath, finalContent, 'utf8');
console.log('Updated ' + filePath);
}
}
function walkDir(d) {
const files = fs.readdirSync(d);
for (const f of files) {
const fullPath = path.join(d, f);
if (fs.statSync(fullPath).isDirectory()) {
walkDir(fullPath);
} else {
processFile(fullPath);
}
}
}
walkDir(dir);