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
58 lines (53 loc) · 2.28 KB
/
envFileParser.ts
File metadata and controls
58 lines (53 loc) · 2.28 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
import * as fs from 'fs';
import * as path from 'path';
type EnvVars = Object & { [key: string]: string };
export function parseEnvFile(envFile: string, mergeWithProcessEnvVars: boolean = true): EnvVars {
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 mergeWithProcessEnvVars ? mergeEnvVariables(env, process.env) : mergePythonPath(env, process.env.PYTHONPATH);
}
/**
* Merge the target environment variables into the source.
* Note: The source variables are modified and returned (i.e. it modifies value passed in).
* @export
* @param {EnvVars} targetEnvVars target environment variables.
* @param {EnvVars} [sourceEnvVars=process.env] source environment variables (defaults to current process variables).
* @returns {EnvVars}
*/
export function mergeEnvVariables(targetEnvVars: EnvVars, sourceEnvVars: EnvVars = process.env): EnvVars {
Object.keys(sourceEnvVars).forEach(setting => {
if (targetEnvVars[setting] === undefined) {
targetEnvVars[setting] = sourceEnvVars[setting];
}
});
return mergePythonPath(targetEnvVars, sourceEnvVars.PYTHONPATH);
}
/**
* Merge the target PYTHONPATH value into the env variables passed.
* Note: The env variables passed in are modified and returned (i.e. it modifies value passed in).
* @export
* @param {EnvVars} env target environment variables.
* @param {string | undefined} [currentPythonPath] PYTHONPATH value.
* @returns {EnvVars}
*/
export function mergePythonPath(env: EnvVars, currentPythonPath: string | undefined): EnvVars {
if (typeof currentPythonPath !== 'string' || currentPythonPath.length === 0) {
return env;
}
if (typeof env.PYTHONPATH === 'string' && env.PYTHONPATH.length > 0) {
env.PYTHONPATH = env.PYTHONPATH + path.delimiter + currentPythonPath;
} else {
env.PYTHONPATH = currentPythonPath;
}
return env;
}