-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
44 lines (39 loc) · 1.22 KB
/
main.js
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
const fs = require('fs');
const path = require('path');
const envPath = path.join(process.cwd(), '.env');
function tinydot(){
try {
const envData = fs.readFileSync(envPath, 'utf8');
const envLines = envData.split('\n');
const envVariables = {};
for (const line of envLines) {
let trimmedLine = line.trim()
if (trimmedLine.startsWith('#') ||!trimmedLine) {
continue;
}
// Remove inline comments
const commentIndex = trimmedLine.indexOf('#');
if (commentIndex > -1) {
trimmedLine = trimmedLine.slice(0, commentIndex).trim();
}
const [key, value] = trimmedLine.split('=');
if (key && value) {
// parse boolean or number
if (value === "true") {
envVariables[key.trim()] = true;
} else if (value === "false") {
envVariables[key.trim()] = false;
} else if (!isNaN(value)) {
envVariables[key.trim()] = Number(value);
} else {
envVariables[key.trim()] = value.trim();
}
}
}
return envVariables
} catch (error) {
console.error(error)
return null
}
}
module.exports = tinydot