1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Diagnostics ;
3
4
using System . IO ;
4
5
using System . Linq ;
@@ -46,6 +47,7 @@ public OutOfProcessNodeInstance(
46
47
string [ ] watchFileExtensions ,
47
48
string commandLineArguments ,
48
49
ILogger nodeOutputLogger ,
50
+ IDictionary < string , string > environmentVars ,
49
51
bool launchWithDebugging ,
50
52
int ? debuggingPort )
51
53
{
@@ -58,7 +60,7 @@ public OutOfProcessNodeInstance(
58
60
_entryPointScript = new StringAsTempFile ( entryPointScript ) ;
59
61
60
62
var startInfo = PrepareNodeProcessStartInfo ( _entryPointScript . FileName , projectPath , commandLineArguments ,
61
- launchWithDebugging , debuggingPort ) ;
63
+ environmentVars , launchWithDebugging , debuggingPort ) ;
62
64
_nodeProcess = LaunchNodeProcess ( startInfo ) ;
63
65
_watchFileExtensions = watchFileExtensions ;
64
66
_fileSystemWatcher = BeginFileWatcher ( projectPath ) ;
@@ -99,7 +101,7 @@ public void Dispose()
99
101
// This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
100
102
protected virtual ProcessStartInfo PrepareNodeProcessStartInfo (
101
103
string entryPointFilename , string projectPath , string commandLineArguments ,
102
- bool launchWithDebugging , int ? debuggingPort )
104
+ IDictionary < string , string > environmentVars , bool launchWithDebugging , int ? debuggingPort )
103
105
{
104
106
string debuggingArgs ;
105
107
if ( launchWithDebugging )
@@ -122,6 +124,19 @@ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
122
124
WorkingDirectory = projectPath
123
125
} ;
124
126
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
+
125
140
// Append projectPath to NODE_PATH so it can locate node_modules
126
141
var existingNodePath = Environment . GetEnvironmentVariable ( "NODE_PATH" ) ?? string . Empty ;
127
142
if ( existingNodePath != string . Empty )
@@ -130,11 +145,7 @@ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
130
145
}
131
146
132
147
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 ) ;
138
149
139
150
return startInfo ;
140
151
}
@@ -179,6 +190,15 @@ private void EnsureFileSystemWatcherIsDisposed()
179
190
}
180
191
}
181
192
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
+
182
202
private static Process LaunchNodeProcess ( ProcessStartInfo startInfo )
183
203
{
184
204
var process = Process . Start ( startInfo ) ;
0 commit comments