-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_env.js
More file actions
50 lines (37 loc) · 1.19 KB
/
fix_env.js
File metadata and controls
50 lines (37 loc) · 1.19 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
const fs = require('fs');
const path = require('path');
const envPath = path.resolve('.env');
const bakPath = path.resolve('.env.bak');
let content = '';
if (fs.existsSync(envPath)) {
content += fs.readFileSync(envPath, 'utf8') + '\n';
}
if (fs.existsSync(bakPath)) {
content += fs.readFileSync(bakPath, 'utf8') + '\n';
}
// Simple parser to dedupe keys (last one wins) and strip quotes
const lines = content.split('\n');
const env = {};
const order = [];
lines.forEach(line => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) return;
const idx = trimmed.indexOf('=');
if (idx === -1) return;
const key = trimmed.substring(0, idx).trim();
let val = trimmed.substring(idx + 1).trim();
// Strip surrounding quotes
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
val = val.substring(1, val.length - 1);
}
if (!env[key]) {
order.push(key);
}
env[key] = val;
});
let newContent = '';
order.forEach(key => {
newContent += `${key}=${env[key]}\n`;
});
fs.writeFileSync(envPath, newContent, 'utf8');
console.log('Fixed .env encoding, deduped keys, and stripped quotes.');