forked from aspnet/JavaScriptServices
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cs
More file actions
51 lines (45 loc) · 2.23 KB
/
Copy pathConfiguration.cs
File metadata and controls
51 lines (45 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.AspNetCore.Hosting;
namespace Microsoft.AspNetCore.NodeServices {
public static class Configuration {
private readonly static string[] defaultWatchFileExtensions = new[] { ".js", ".jsx", ".ts", ".tsx", ".json", ".html" };
private readonly static NodeServicesOptions defaultOptions = new NodeServicesOptions {
HostingModel = NodeHostingModel.Http,
WatchFileExtensions = defaultWatchFileExtensions
};
public static void AddNodeServices(this IServiceCollection serviceCollection) {
AddNodeServices(serviceCollection, defaultOptions);
}
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeServicesOptions options) {
serviceCollection.AddSingleton(typeof(INodeServices), (serviceProvider) => {
var hostEnv = serviceProvider.GetRequiredService<IHostingEnvironment>();
if (string.IsNullOrEmpty(options.ProjectPath)) {
options.ProjectPath = hostEnv.ContentRootPath;
}
return CreateNodeServices(options);
});
}
public static INodeServices CreateNodeServices(NodeServicesOptions options)
{
var watchFileExtensions = options.WatchFileExtensions ?? defaultWatchFileExtensions;
switch (options.HostingModel)
{
case NodeHostingModel.Http:
return new HttpNodeInstance(options.ProjectPath, /* port */ 0, watchFileExtensions);
case NodeHostingModel.InputOutputStream:
return new InputOutputStreamNodeInstance(options.ProjectPath);
default:
throw new System.ArgumentException("Unknown hosting model: " + options.HostingModel.ToString());
}
}
}
public class NodeServicesOptions {
public NodeHostingModel HostingModel { get; set; }
public string ProjectPath { get; set; }
public string[] WatchFileExtensions { get; set; }
public NodeServicesOptions() {
this.HostingModel = NodeHostingModel.Http;
}
}
}