forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvFileParser.ts
More file actions
27 lines (24 loc) · 898 Bytes
/
envFileParser.ts
File metadata and controls
27 lines (24 loc) · 898 Bytes
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
import * as fs from 'fs';
export function parseEnvFile(envFile: string): any {
const buffer = fs.readFileSync(envFile, 'utf8');
const env = {};
buffer.split('\n').forEach(line => {
const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (r !== null) {
let value = r[2] || '';
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
value = value.replace(/\\n/gm, '\n');
}
env[r[1]] = value.replace(/(^['"]|['"]$)/g, '');
}
});
return mergeEnvVariables(env);
}
export function mergeEnvVariables(newVariables: { [key: string]: string }, mergeWith: any = process.env): any {
for (let setting in mergeWith) {
if (!newVariables[setting]) {
newVariables[setting] = mergeWith[setting];
}
}
return newVariables;
}