Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/3 Code Health/1376.md
Original file line number Diff line number Diff line change
@@ -0,0 +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).
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,7 @@
"dependencies": {
"arch": "2.1.0",
"diff-match-patch": "1.0.0",
"dotenv": "^5.0.1",
"fs-extra": "4.0.3",
"fuzzy": "0.1.3",
"get-port": "3.2.0",
Expand Down Expand Up @@ -1901,6 +1902,7 @@
"@types/chai": "^4.1.2",
"@types/chai-as-promised": "^7.1.0",
"@types/del": "^3.0.0",
"@types/dotenv": "^4.0.3",
"@types/event-stream": "^3.3.33",
"@types/fs-extra": "^5.0.1",
"@types/get-port": "^3.2.0",
Expand Down
31 changes: 3 additions & 28 deletions src/client/common/variables/environment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as dotenv from 'dotenv';
import * as fs from 'fs-extra';
import { inject, injectable } from 'inversify';
import * as path from 'path';
Expand All @@ -10,7 +11,7 @@ import { EnvironmentVariables, IEnvironmentVariablesService } from './types';
@injectable()
export class EnvironmentVariablesService implements IEnvironmentVariablesService {
private readonly pathVariable: 'PATH' | 'Path';
constructor( @inject(IPathUtils) pathUtils: IPathUtils) {
constructor(@inject(IPathUtils) pathUtils: IPathUtils) {
this.pathVariable = pathUtils.getPathVariableName();
}
public async parseFile(filePath: string): Promise<EnvironmentVariables | undefined> {
Expand All @@ -21,14 +22,7 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
if (!fs.lstatSync(filePath).isFile()) {
return undefined;
}
return new Promise<EnvironmentVariables | undefined>((resolve, reject) => {
fs.readFile(filePath, 'utf8', (error, data) => {
if (error) {
return reject(error);
}
resolve(parseEnvironmentVariables(data));
});
});
return dotenv.parse(filePath);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! This package - does it deal with multiple encoding then?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes and no. We basically need tell it the encoding that needs to be used.

}
public mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables) {
if (!target) {
Expand Down Expand Up @@ -67,22 +61,3 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
return vars;
}
}

function parseEnvironmentVariables(contents: string): EnvironmentVariables | undefined {
if (typeof contents !== 'string' || contents.length === 0) {
return undefined;
}

const env = {} as EnvironmentVariables;
contents.split('\n').forEach(line => {
const match = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (match !== null) {
let value = typeof match[2] === 'string' ? match[2] : '';
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
value = value.replace(/\\n/gm, '\n');
}
env[match[1]] = value.replace(/(^['"]|['"]$)/g, '');
}
});
return env;
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
dependencies:
"@types/glob" "*"

"@types/dotenv@^4.0.3":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-4.0.3.tgz#ebcfc40da7bc0728b705945b7db48485ec5b4b67"
dependencies:
"@types/node" "*"

"@types/event-stream@^3.3.33":
version "3.3.33"
resolved "https://registry.yarnpkg.com/@types/event-stream/-/event-stream-3.3.33.tgz#ca155f6e805b606175322c03e6d75bc3b940cb95"
Expand Down Expand Up @@ -1061,6 +1067,10 @@ doctrine@0.7.2:
esutils "^1.1.6"
isarray "0.0.1"

dotenv@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef"

duplexer2@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.0.2.tgz#c614dcf67e2fb14995a91711e5a617e8a60a31db"
Expand Down