Skip to content

Commit ff674b8

Browse files
authored
Use dotenv package to parse environment variables definition files (DonJayamanne#1802)
1 parent c4f26eb commit ff674b8

4 files changed

Lines changed: 16 additions & 28 deletions

File tree

news/3 Code Health/1376.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use [dotenv](https://www.npmjs.com/package/dotenv) package to parse [environment variables definition files](https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file).

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,7 @@
18641864
"dependencies": {
18651865
"arch": "2.1.0",
18661866
"diff-match-patch": "1.0.0",
1867+
"dotenv": "^5.0.1",
18671868
"fs-extra": "4.0.3",
18681869
"fuzzy": "0.1.3",
18691870
"get-port": "3.2.0",
@@ -1902,6 +1903,7 @@
19021903
"@types/chai-arrays": "^1.0.2",
19031904
"@types/chai-as-promised": "^7.1.0",
19041905
"@types/del": "^3.0.0",
1906+
"@types/dotenv": "^4.0.3",
19051907
"@types/event-stream": "^3.3.33",
19061908
"@types/fs-extra": "^5.0.1",
19071909
"@types/get-port": "^3.2.0",

src/client/common/variables/environment.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
import * as dotenv from 'dotenv';
45
import * as fs from 'fs-extra';
56
import { inject, injectable } from 'inversify';
67
import * as path from 'path';
@@ -10,7 +11,7 @@ import { EnvironmentVariables, IEnvironmentVariablesService } from './types';
1011
@injectable()
1112
export class EnvironmentVariablesService implements IEnvironmentVariablesService {
1213
private readonly pathVariable: 'PATH' | 'Path';
13-
constructor( @inject(IPathUtils) pathUtils: IPathUtils) {
14+
constructor(@inject(IPathUtils) pathUtils: IPathUtils) {
1415
this.pathVariable = pathUtils.getPathVariableName();
1516
}
1617
public async parseFile(filePath: string): Promise<EnvironmentVariables | undefined> {
@@ -21,14 +22,7 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
2122
if (!fs.lstatSync(filePath).isFile()) {
2223
return undefined;
2324
}
24-
return new Promise<EnvironmentVariables | undefined>((resolve, reject) => {
25-
fs.readFile(filePath, 'utf8', (error, data) => {
26-
if (error) {
27-
return reject(error);
28-
}
29-
resolve(parseEnvironmentVariables(data));
30-
});
31-
});
25+
return dotenv.parse(filePath);
3226
}
3327
public mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables) {
3428
if (!target) {
@@ -67,22 +61,3 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
6761
return vars;
6862
}
6963
}
70-
71-
function parseEnvironmentVariables(contents: string): EnvironmentVariables | undefined {
72-
if (typeof contents !== 'string' || contents.length === 0) {
73-
return undefined;
74-
}
75-
76-
const env = {} as EnvironmentVariables;
77-
contents.split('\n').forEach(line => {
78-
const match = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
79-
if (match !== null) {
80-
let value = typeof match[2] === 'string' ? match[2] : '';
81-
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
82-
value = value.replace(/\\n/gm, '\n');
83-
}
84-
env[match[1]] = value.replace(/(^['"]|['"]$)/g, '');
85-
}
86-
});
87-
return env;
88-
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
dependencies:
5454
"@types/glob" "*"
5555

56+
"@types/dotenv@^4.0.3":
57+
version "4.0.3"
58+
resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-4.0.3.tgz#ebcfc40da7bc0728b705945b7db48485ec5b4b67"
59+
dependencies:
60+
"@types/node" "*"
61+
5662
"@types/event-stream@^3.3.33":
5763
version "3.3.33"
5864
resolved "https://registry.yarnpkg.com/@types/event-stream/-/event-stream-3.3.33.tgz#ca155f6e805b606175322c03e6d75bc3b940cb95"
@@ -1071,6 +1077,10 @@ doctrine@0.7.2:
10711077
esutils "^1.1.6"
10721078
isarray "0.0.1"
10731079

1080+
dotenv@^5.0.1:
1081+
version "5.0.1"
1082+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef"
1083+
10741084
duplexer2@0.0.2:
10751085
version "0.0.2"
10761086
resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.0.2.tgz#c614dcf67e2fb14995a91711e5a617e8a60a31db"

0 commit comments

Comments
 (0)