diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs index 749118f..55efaeb 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs @@ -32,15 +32,14 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels private bool _disposed; private int _portNumber; - public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, Action onBeforeStartExternalProcess = null) + public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0) : base( EmbeddedResourceReader.Read( typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), projectPath, watchFileExtensions, - MakeCommandLineOptions(port), - onBeforeStartExternalProcess) + MakeCommandLineOptions(port)) { _client = new HttpClient(); } diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs index 3690a47..a7cb161 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs @@ -31,11 +31,12 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels string entryPointScript, string projectPath, string[] watchFileExtensions, - string commandLineArguments, - Action onBeforeStartExternalProcess = null) + string commandLineArguments) { _entryPointScript = new StringAsTempFile(entryPointScript); - _nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments, onBeforeStartExternalProcess); + + var startInfo = PrepareNodeProcessStartInfo(_entryPointScript.FileName, projectPath, commandLineArguments); + _nodeProcess = LaunchNodeProcess(startInfo); _watchFileExtensions = watchFileExtensions; _fileSystemWatcher = BeginFileWatcher(projectPath); ConnectToInputOutputStreams(); @@ -72,6 +73,37 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels protected abstract Task InvokeExportAsync(NodeInvocationInfo invocationInfo); + // This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe + protected virtual ProcessStartInfo PrepareNodeProcessStartInfo( + string entryPointFilename, string projectPath, string commandLineArguments) + { + var startInfo = new ProcessStartInfo("node") + { + Arguments = "\"" + entryPointFilename + "\" " + (commandLineArguments ?? string.Empty), + UseShellExecute = false, + RedirectStandardInput = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + WorkingDirectory = projectPath + }; + + // Append projectPath to NODE_PATH so it can locate node_modules + var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty; + if (existingNodePath != string.Empty) + { + existingNodePath += ":"; + } + + var nodePathValue = existingNodePath + Path.Combine(projectPath, "node_modules"); +#if NET451 + startInfo.EnvironmentVariables["NODE_PATH"] = nodePathValue; +#else + startInfo.Environment["NODE_PATH"] = nodePathValue; +#endif + + return startInfo; + } + protected virtual void OnOutputDataReceived(string outputData) { Console.WriteLine("[Node] " + outputData); @@ -112,36 +144,8 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels } } - private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments, Action onBeforeStartExternalProcess) + private static Process LaunchNodeProcess(ProcessStartInfo startInfo) { - var startInfo = new ProcessStartInfo("node") - { - Arguments = "\"" + entryPointFilename + "\" " + (commandLineArguments ?? string.Empty), - UseShellExecute = false, - RedirectStandardInput = true, - RedirectStandardOutput = true, - RedirectStandardError = true, - WorkingDirectory = projectPath - }; - - // Append projectPath to NODE_PATH so it can locate node_modules - var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty; - if (existingNodePath != string.Empty) - { - existingNodePath += ":"; - } - - var nodePathValue = existingNodePath + Path.Combine(projectPath, "node_modules"); -#if NET451 - startInfo.EnvironmentVariables["NODE_PATH"] = nodePathValue; -#else - startInfo.Environment["NODE_PATH"] = nodePathValue; -#endif - if (onBeforeStartExternalProcess != null) - { - onBeforeStartExternalProcess(startInfo); - } - var process = Process.Start(startInfo); // On Mac at least, a killed child process is left open as a zombie until the parent diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs index bb2611e..114e0ea 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs @@ -36,14 +36,13 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels private string _socketAddress; private VirtualConnectionClient _virtualConnectionClient; - public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, Action onBeforeStartExternalProcess = null) : base( + public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress) : base( EmbeddedResourceReader.Read( typeof(SocketNodeInstance), "/Content/Node/entrypoint-socket.js"), projectPath, watchFileExtensions, - MakeNewCommandLineOptions(socketAddress), - onBeforeStartExternalProcess) + MakeNewCommandLineOptions(socketAddress)) { _socketAddress = socketAddress; }