mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 10:08:57 +00:00
Change onBeforeStartExternalProcess to a virtual method, so as to avoid expanding the set of constructor params in all hosting models
This commit is contained in:
@@ -32,15 +32,14 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
|
|||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
private int _portNumber;
|
private int _portNumber;
|
||||||
|
|
||||||
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
|
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0)
|
||||||
: base(
|
: base(
|
||||||
EmbeddedResourceReader.Read(
|
EmbeddedResourceReader.Read(
|
||||||
typeof(HttpNodeInstance),
|
typeof(HttpNodeInstance),
|
||||||
"/Content/Node/entrypoint-http.js"),
|
"/Content/Node/entrypoint-http.js"),
|
||||||
projectPath,
|
projectPath,
|
||||||
watchFileExtensions,
|
watchFileExtensions,
|
||||||
MakeCommandLineOptions(port),
|
MakeCommandLineOptions(port))
|
||||||
onBeforeStartExternalProcess)
|
|
||||||
{
|
{
|
||||||
_client = new HttpClient();
|
_client = new HttpClient();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,11 +31,12 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
|
|||||||
string entryPointScript,
|
string entryPointScript,
|
||||||
string projectPath,
|
string projectPath,
|
||||||
string[] watchFileExtensions,
|
string[] watchFileExtensions,
|
||||||
string commandLineArguments,
|
string commandLineArguments)
|
||||||
Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
|
|
||||||
{
|
{
|
||||||
_entryPointScript = new StringAsTempFile(entryPointScript);
|
_entryPointScript = new StringAsTempFile(entryPointScript);
|
||||||
_nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments, onBeforeStartExternalProcess);
|
|
||||||
|
var startInfo = PrepareNodeProcessStartInfo(_entryPointScript.FileName, projectPath, commandLineArguments);
|
||||||
|
_nodeProcess = LaunchNodeProcess(startInfo);
|
||||||
_watchFileExtensions = watchFileExtensions;
|
_watchFileExtensions = watchFileExtensions;
|
||||||
_fileSystemWatcher = BeginFileWatcher(projectPath);
|
_fileSystemWatcher = BeginFileWatcher(projectPath);
|
||||||
ConnectToInputOutputStreams();
|
ConnectToInputOutputStreams();
|
||||||
@@ -72,6 +73,37 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
|
|||||||
|
|
||||||
protected abstract Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocationInfo);
|
protected abstract Task<T> InvokeExportAsync<T>(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)
|
protected virtual void OnOutputDataReceived(string outputData)
|
||||||
{
|
{
|
||||||
Console.WriteLine("[Node] " + 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<System.Diagnostics.ProcessStartInfo> 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);
|
var process = Process.Start(startInfo);
|
||||||
|
|
||||||
// On Mac at least, a killed child process is left open as a zombie until the parent
|
// On Mac at least, a killed child process is left open as a zombie until the parent
|
||||||
|
|||||||
@@ -36,14 +36,13 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
|
|||||||
private string _socketAddress;
|
private string _socketAddress;
|
||||||
private VirtualConnectionClient _virtualConnectionClient;
|
private VirtualConnectionClient _virtualConnectionClient;
|
||||||
|
|
||||||
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null) : base(
|
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress) : base(
|
||||||
EmbeddedResourceReader.Read(
|
EmbeddedResourceReader.Read(
|
||||||
typeof(SocketNodeInstance),
|
typeof(SocketNodeInstance),
|
||||||
"/Content/Node/entrypoint-socket.js"),
|
"/Content/Node/entrypoint-socket.js"),
|
||||||
projectPath,
|
projectPath,
|
||||||
watchFileExtensions,
|
watchFileExtensions,
|
||||||
MakeNewCommandLineOptions(socketAddress),
|
MakeNewCommandLineOptions(socketAddress))
|
||||||
onBeforeStartExternalProcess)
|
|
||||||
{
|
{
|
||||||
_socketAddress = socketAddress;
|
_socketAddress = socketAddress;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user