Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 3305dd1

Browse files
committed
Implement more robust env variable processing
1 parent fc413eb commit 3305dd1

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

client/src/util/env.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,35 @@ function getShim(): string {
3333
return shimPath;
3434
}
3535

36+
// Based on the dotenv parse function:
37+
// https://github.com/motdotla/dotenv/blob/master/lib/main.js#L32
38+
// modified to not have to deal with Buffers and to handle stuff
39+
// like export and declare -x at the start of the line
40+
function processExportLine(line: string): string[] {
41+
const result = [];
42+
// matching "KEY' and 'VAL' in 'KEY=VAL' with support for arbitrary prefixes
43+
const keyValueArr = line.match(/^(?:[\w-]*\s+)*([\w.-]+)\s*=\s*(.*)?\s*$/);
44+
if (keyValueArr != null) {
45+
const key = keyValueArr[1];
46+
47+
// default undefined or missing values to empty string
48+
let value = keyValueArr[2] || '';
49+
50+
// expand newlines in quoted values
51+
const len = value ? value.length : 0;
52+
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
53+
value = value.replace(/\\n/gm, '\n');
54+
}
55+
56+
// remove any surrounding quotes and extra spaces
57+
value = value.replace(/(^['"]|['"]$)/g, '').trim();
58+
59+
result.push(key, value);
60+
}
61+
62+
return result;
63+
}
64+
3665
const RUBY_ENVIRONMENT_VARIABLES = [
3766
'PATH',
3867
'RUBY_VERSION',
@@ -61,14 +90,15 @@ export interface IEnvironment {
6190

6291
export function loadEnv(cwd: string): RubyEnvironment {
6392
const shim: string = getShim();
93+
const env: IEnvironment = {};
6494
const { stdout, stderr } = execa.sync(shim, [], {
6595
cwd,
6696
});
6797

6898
console.error(stderr);
6999

70-
for (const envVar of stdout.split('\n')) {
71-
const result: string[] = envVar.split('=', 2);
100+
for (const line of stdout.split('\n')) {
101+
const result: string[] = processExportLine(line);
72102
if (RUBY_ENVIRONMENT_VARIABLES.indexOf(result[0]) >= 0) {
73103
env[result[0]] = result[1];
74104
}

0 commit comments

Comments
 (0)