-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-config.js
More file actions
82 lines (68 loc) · 3.01 KB
/
fix-config.js
File metadata and controls
82 lines (68 loc) · 3.01 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
const fs = require('fs');
const path = require('path');
// Mapping of process.env usage to config properties
const configMappings = [
{ pattern: /process\.env\.WORKER_CONCURRENCY/g, replacement: 'config.workerConcurrency' },
{ pattern: /process\.env\.NODE_ENV/g, replacement: 'config.nodeEnv' },
{ pattern: /process\.env\.LOG_LEVEL/g, replacement: 'config.logLevel' },
{ pattern: /process\.env\.API_PORT/g, replacement: 'config.port' },
{ pattern: /process\.env\.REDIS_HOST/g, replacement: 'config.redis.host' },
{ pattern: /process\.env\.REDIS_PORT/g, replacement: 'config.redis.port' },
{ pattern: /process\.env\.PROXY_LIST/g, replacement: 'config.proxyList' }
];
// Get all TypeScript files in src except config.ts itself
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.')
&& !entry.includes('config.ts')) {
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 process.env usage (excluding config file)
if (/process\.env\./.test(content) && !filePath.includes('config.ts')) {
// Add config import if needed
if (!content.includes('from \'../utils/config\'') && !content.includes('from \'../../utils/config\'')) {
// Determine relative path to config
const relativePath = path.relative(path.dirname(filePath), './src/utils/config').replace(/\\/g, '/');
const importPath = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
const importLine = `import { config } from '${importPath}'\n`;
// Add import after existing imports
const lastImportMatch = content.match(/^import.*$/gm);
if (lastImportMatch) {
const lastImport = lastImportMatch[lastImportMatch.length - 1];
content = content.replace(lastImport, lastImport + '\n' + importLine);
changed = true;
}
}
// Apply config replacements
for (const mapping of configMappings) {
const beforeCount = (content.match(mapping.pattern) || []).length;
content = content.replace(mapping.pattern, mapping.replacement);
const afterCount = (content.match(mapping.pattern) || []).length;
if (beforeCount > afterCount) {
changed = true;
totalReplacements += (beforeCount - afterCount);
}
}
if (changed) {
fs.writeFileSync(filePath, content);
console.log(`Updated ${filePath}`);
}
}
}
console.log(`Total process.env replacements: ${totalReplacements}`);
console.log('Note: Some process.env usage might need manual review');