Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Adding support for capturing the output of a node instance for custom… #184

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ private static INodeInstance CreateNodeInstance(NodeServicesOptions options)
switch (options.HostingModel)
{
case NodeHostingModel.Http:
return new HttpNodeInstance(options.ProjectPath, options.WatchFileExtensions, /* port */ 0);
return new HttpNodeInstance(options.ProjectPath, options.WatchFileExtensions, /* port */ 0, options.NodeInstanceOutputLogger);
case NodeHostingModel.Socket:
var pipeName = "pni-" + Guid.NewGuid().ToString("D"); // Arbitrary non-clashing string
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions, pipeName);
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions, pipeName, options.NodeInstanceOutputLogger);
default:
throw new ArgumentException("Unknown hosting model: " + options.HostingModel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.AspNetCore.NodeServices.HostingModels;
using Microsoft.AspNetCore.NodeServices.Util;

namespace Microsoft.AspNetCore.NodeServices
{
Expand All @@ -19,5 +20,6 @@ public NodeServicesOptions()
public Func<INodeInstance> NodeInstanceFactory { get; set; }
public string ProjectPath { get; set; }
public string[] WatchFileExtensions { get; set; }
public INodeInstanceOutputLogger NodeInstanceOutputLogger { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down Expand Up @@ -32,14 +33,15 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
private bool _disposed;
private int _portNumber;

public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0)
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, INodeInstanceOutputLogger nodeInstanceOutputLogger = null)
: base(
EmbeddedResourceReader.Read(
typeof(HttpNodeInstance),
"/Content/Node/entrypoint-http.js"),
projectPath,
watchFileExtensions,
MakeCommandLineOptions(port))
MakeCommandLineOptions(port),
nodeInstanceOutputLogger)
{
_client = new HttpClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices.Util;

namespace Microsoft.AspNetCore.NodeServices.HostingModels
{
Expand All @@ -26,13 +27,18 @@ public abstract class OutOfProcessNodeInstance : INodeInstance
private readonly Process _nodeProcess;
private bool _nodeProcessNeedsRestart;
private readonly string[] _watchFileExtensions;
private INodeInstanceOutputLogger _nodeInstanceOutputLogger;

public OutOfProcessNodeInstance(
string entryPointScript,
string projectPath,
string[] watchFileExtensions,
string commandLineArguments)
string commandLineArguments,
INodeInstanceOutputLogger nodeOutputLogger)
{
_nodeInstanceOutputLogger = nodeOutputLogger;
if(_nodeInstanceOutputLogger == null)
_nodeInstanceOutputLogger = new ConsoleNodeInstanceOutputLogger();
_entryPointScript = new StringAsTempFile(entryPointScript);
_nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments);
_watchFileExtensions = watchFileExtensions;
Expand Down Expand Up @@ -73,12 +79,12 @@ public void Dispose()

protected virtual void OnOutputDataReceived(string outputData)
{
Console.WriteLine("[Node] " + outputData);
_nodeInstanceOutputLogger.LogOutputData(outputData);
}

protected virtual void OnErrorDataReceived(string errorData)
{
Console.WriteLine("[Node] " + errorData);
_nodeInstanceOutputLogger.LogErrorData(errorData);
}

protected virtual void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices.HostingModels.PhysicalConnections;
using Microsoft.AspNetCore.NodeServices.HostingModels.VirtualConnections;
using Microsoft.AspNetCore.NodeServices.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down Expand Up @@ -36,13 +37,14 @@ internal class SocketNodeInstance : OutOfProcessNodeInstance
private string _socketAddress;
private VirtualConnectionClient _virtualConnectionClient;

public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress): base(
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, INodeInstanceOutputLogger nodeInstanceOutputLogger = null) : base(
EmbeddedResourceReader.Read(
typeof(SocketNodeInstance),
"/Content/Node/entrypoint-socket.js"),
projectPath,
watchFileExtensions,
MakeNewCommandLineOptions(socketAddress))
MakeNewCommandLineOptions(socketAddress),
nodeInstanceOutputLogger)
{
_socketAddress = socketAddress;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Microsoft.AspNetCore.NodeServices.Util
{
public class ConsoleNodeInstanceOutputLogger : INodeInstanceOutputLogger
{
public void LogOutputData(string outputData)
{
Console.WriteLine("[Node] " + outputData);
}

public void LogErrorData(string errorData)
{
Console.WriteLine("[Node] " + errorData);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.NodeServices.Util
{
public interface INodeInstanceOutputLogger
{
void LogOutputData(string outputData);

void LogErrorData(string errorData);
}
}