11using System ;
2+ using System . Collections . Generic ;
23using System . Diagnostics ;
34using System . IO ;
45using System . Linq ;
@@ -46,6 +47,7 @@ public OutOfProcessNodeInstance(
4647 string [ ] watchFileExtensions ,
4748 string commandLineArguments ,
4849 ILogger nodeOutputLogger ,
50+ IDictionary < string , string > environmentVars ,
4951 bool launchWithDebugging ,
5052 int ? debuggingPort )
5153 {
@@ -58,7 +60,7 @@ public OutOfProcessNodeInstance(
5860 _entryPointScript = new StringAsTempFile ( entryPointScript ) ;
5961
6062 var startInfo = PrepareNodeProcessStartInfo ( _entryPointScript . FileName , projectPath , commandLineArguments ,
61- launchWithDebugging , debuggingPort ) ;
63+ environmentVars , launchWithDebugging , debuggingPort ) ;
6264 _nodeProcess = LaunchNodeProcess ( startInfo ) ;
6365 _watchFileExtensions = watchFileExtensions ;
6466 _fileSystemWatcher = BeginFileWatcher ( projectPath ) ;
@@ -99,7 +101,7 @@ public void Dispose()
99101 // This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
100102 protected virtual ProcessStartInfo PrepareNodeProcessStartInfo (
101103 string entryPointFilename , string projectPath , string commandLineArguments ,
102- bool launchWithDebugging , int ? debuggingPort )
104+ IDictionary < string , string > environmentVars , bool launchWithDebugging , int ? debuggingPort )
103105 {
104106 string debuggingArgs ;
105107 if ( launchWithDebugging )
@@ -122,6 +124,19 @@ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
122124 WorkingDirectory = projectPath
123125 } ;
124126
127+ // Append environment vars
128+ if ( environmentVars != null )
129+ {
130+ foreach ( var envVarKey in environmentVars . Keys )
131+ {
132+ var envVarValue = environmentVars [ envVarKey ] ;
133+ if ( envVarValue != null )
134+ {
135+ SetEnvironmentVariable ( startInfo , envVarKey , envVarValue ) ;
136+ }
137+ }
138+ }
139+
125140 // Append projectPath to NODE_PATH so it can locate node_modules
126141 var existingNodePath = Environment . GetEnvironmentVariable ( "NODE_PATH" ) ?? string . Empty ;
127142 if ( existingNodePath != string . Empty )
@@ -130,11 +145,7 @@ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
130145 }
131146
132147 var nodePathValue = existingNodePath + Path . Combine ( projectPath , "node_modules" ) ;
133- #if NET451
134- startInfo . EnvironmentVariables [ "NODE_PATH" ] = nodePathValue ;
135- #else
136- startInfo . Environment [ "NODE_PATH" ] = nodePathValue ;
137- #endif
148+ SetEnvironmentVariable ( startInfo , "NODE_PATH" , nodePathValue ) ;
138149
139150 return startInfo ;
140151 }
@@ -179,6 +190,15 @@ private void EnsureFileSystemWatcherIsDisposed()
179190 }
180191 }
181192
193+ private static void SetEnvironmentVariable ( ProcessStartInfo startInfo , string name , string value )
194+ {
195+ #if NET451
196+ startInfo . EnvironmentVariables [ name ] = value ;
197+ #else
198+ startInfo . Environment [ name ] = value ;
199+ #endif
200+ }
201+
182202 private static Process LaunchNodeProcess ( ProcessStartInfo startInfo )
183203 {
184204 var process = Process . Start ( startInfo ) ;
0 commit comments