Skip to content

Commit a14d9ba

Browse files
Change onBeforeStartExternalProcess to a virtual method, so as to avoid expanding the set of constructor params in all hosting models
1 parent 7119815 commit a14d9ba

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
3232
private bool _disposed;
3333
private int _portNumber;
3434

35-
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
35+
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0)
3636
: base(
3737
EmbeddedResourceReader.Read(
3838
typeof(HttpNodeInstance),
3939
"/Content/Node/entrypoint-http.js"),
4040
projectPath,
4141
watchFileExtensions,
42-
MakeCommandLineOptions(port),
43-
onBeforeStartExternalProcess)
42+
MakeCommandLineOptions(port))
4443
{
4544
_client = new HttpClient();
4645
}

src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ internal class SocketNodeInstance : OutOfProcessNodeInstance
3636
private string _socketAddress;
3737
private VirtualConnectionClient _virtualConnectionClient;
3838

39-
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null) : base(
39+
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress) : base(
4040
EmbeddedResourceReader.Read(
4141
typeof(SocketNodeInstance),
4242
"/Content/Node/entrypoint-socket.js"),
4343
projectPath,
4444
watchFileExtensions,
45-
MakeNewCommandLineOptions(socketAddress),
46-
onBeforeStartExternalProcess)
45+
MakeNewCommandLineOptions(socketAddress))
4746
{
4847
_socketAddress = socketAddress;
4948
}

0 commit comments

Comments
 (0)