@@ -31,11 +31,12 @@ public OutOfProcessNodeInstance(
3131 string entryPointScript ,
3232 string projectPath ,
3333 string [ ] watchFileExtensions ,
34- string commandLineArguments ,
35- Action < System . Diagnostics . ProcessStartInfo > onBeforeStartExternalProcess = null )
34+ string commandLineArguments )
3635 {
3736 _entryPointScript = new StringAsTempFile ( entryPointScript ) ;
38- _nodeProcess = LaunchNodeProcess ( _entryPointScript . FileName , projectPath , commandLineArguments , onBeforeStartExternalProcess ) ;
37+
38+ var startInfo = PrepareNodeProcessStartInfo ( _entryPointScript . FileName , projectPath , commandLineArguments ) ;
39+ _nodeProcess = LaunchNodeProcess ( startInfo ) ;
3940 _watchFileExtensions = watchFileExtensions ;
4041 _fileSystemWatcher = BeginFileWatcher ( projectPath ) ;
4142 ConnectToInputOutputStreams ( ) ;
@@ -72,6 +73,37 @@ public void Dispose()
7273
7374 protected abstract Task < T > InvokeExportAsync < T > ( NodeInvocationInfo invocationInfo ) ;
7475
76+ // This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
77+ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo (
78+ string entryPointFilename , string projectPath , string commandLineArguments )
79+ {
80+ var startInfo = new ProcessStartInfo ( "node" )
81+ {
82+ Arguments = "\" " + entryPointFilename + "\" " + ( commandLineArguments ?? string . Empty ) ,
83+ UseShellExecute = false ,
84+ RedirectStandardInput = true ,
85+ RedirectStandardOutput = true ,
86+ RedirectStandardError = true ,
87+ WorkingDirectory = projectPath
88+ } ;
89+
90+ // Append projectPath to NODE_PATH so it can locate node_modules
91+ var existingNodePath = Environment . GetEnvironmentVariable ( "NODE_PATH" ) ?? string . Empty ;
92+ if ( existingNodePath != string . Empty )
93+ {
94+ existingNodePath += ":" ;
95+ }
96+
97+ var nodePathValue = existingNodePath + Path . Combine ( projectPath , "node_modules" ) ;
98+ #if NET451
99+ startInfo . EnvironmentVariables [ "NODE_PATH" ] = nodePathValue ;
100+ #else
101+ startInfo . Environment [ "NODE_PATH" ] = nodePathValue ;
102+ #endif
103+
104+ return startInfo ;
105+ }
106+
75107 protected virtual void OnOutputDataReceived ( string outputData )
76108 {
77109 Console . WriteLine ( "[Node] " + outputData ) ;
@@ -112,36 +144,8 @@ private void EnsureFileSystemWatcherIsDisposed()
112144 }
113145 }
114146
115- private static Process LaunchNodeProcess ( string entryPointFilename , string projectPath , string commandLineArguments , Action < System . Diagnostics . ProcessStartInfo > onBeforeStartExternalProcess )
147+ private static Process LaunchNodeProcess ( ProcessStartInfo startInfo )
116148 {
117- var startInfo = new ProcessStartInfo ( "node" )
118- {
119- Arguments = "\" " + entryPointFilename + "\" " + ( commandLineArguments ?? string . Empty ) ,
120- UseShellExecute = false ,
121- RedirectStandardInput = true ,
122- RedirectStandardOutput = true ,
123- RedirectStandardError = true ,
124- WorkingDirectory = projectPath
125- } ;
126-
127- // Append projectPath to NODE_PATH so it can locate node_modules
128- var existingNodePath = Environment . GetEnvironmentVariable ( "NODE_PATH" ) ?? string . Empty ;
129- if ( existingNodePath != string . Empty )
130- {
131- existingNodePath += ":" ;
132- }
133-
134- var nodePathValue = existingNodePath + Path . Combine ( projectPath , "node_modules" ) ;
135- #if NET451
136- startInfo . EnvironmentVariables [ "NODE_PATH" ] = nodePathValue ;
137- #else
138- startInfo . Environment [ "NODE_PATH" ] = nodePathValue ;
139- #endif
140- if ( onBeforeStartExternalProcess != null )
141- {
142- onBeforeStartExternalProcess ( startInfo ) ;
143- }
144-
145149 var process = Process . Start ( startInfo ) ;
146150
147151 // On Mac at least, a killed child process is left open as a zombie until the parent
0 commit comments