11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4+ import * as dotenv from 'dotenv' ;
45import * as fs from 'fs-extra' ;
56import { inject , injectable } from 'inversify' ;
67import * as path from 'path' ;
@@ -10,7 +11,7 @@ import { EnvironmentVariables, IEnvironmentVariablesService } from './types';
1011@injectable ( )
1112export 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- }
0 commit comments