22using System . Collections . Generic ;
33using Microsoft . AspNetCore . NodeServices . HostingModels ;
44using Microsoft . Extensions . Logging ;
5+ using Microsoft . Extensions . DependencyInjection ;
6+ using Microsoft . AspNetCore . Hosting ;
7+ using Microsoft . Extensions . Logging . Console ;
58
69namespace Microsoft . AspNetCore . NodeServices
710{
811 public class NodeServicesOptions
912 {
1013 public const NodeHostingModel DefaultNodeHostingModel = NodeHostingModel . Http ;
11-
14+ private const string LogCategoryName = "Microsoft.AspNetCore.NodeServices" ;
1215 private static readonly string [ ] DefaultWatchFileExtensions = { ".js" , ".jsx" , ".ts" , ".tsx" , ".json" , ".html" } ;
1316
14- public NodeServicesOptions ( )
17+ public NodeServicesOptions ( IServiceProvider serviceProvider )
1518 {
19+ if ( serviceProvider == null )
20+ {
21+ throw new ArgumentNullException ( nameof ( serviceProvider ) ) ;
22+ }
23+
24+ EnvironmentVariables = new Dictionary < string , string > ( ) ;
1625 HostingModel = DefaultNodeHostingModel ;
1726 WatchFileExtensions = ( string [ ] ) DefaultWatchFileExtensions . Clone ( ) ;
27+
28+ // In an ASP.NET environment, we can use the IHostingEnvironment data to auto-populate a few
29+ // things that you'd otherwise have to specify manually
30+ var hostEnv = serviceProvider . GetService < IHostingEnvironment > ( ) ;
31+ if ( hostEnv != null )
32+ {
33+ ProjectPath = hostEnv . ContentRootPath ;
34+ EnvironmentVariables [ "NODE_ENV" ] = hostEnv . IsDevelopment ( ) ? "development" : "production" ; // De-facto standard values for Node
35+ }
36+
37+ // If the DI system gives us a logger, use it. Otherwise, set up a default one.
38+ var loggerFactory = serviceProvider . GetService < ILoggerFactory > ( ) ;
39+ NodeInstanceOutputLogger = loggerFactory != null
40+ ? loggerFactory . CreateLogger ( LogCategoryName )
41+ : new ConsoleLogger ( LogCategoryName , null , false ) ;
1842 }
19- public Action < System . Diagnostics . ProcessStartInfo > OnBeforeStartExternalProcess { get ; set ; }
43+
2044 public NodeHostingModel HostingModel { get ; set ; }
2145 public Func < INodeInstance > NodeInstanceFactory { get ; set ; }
2246 public string ProjectPath { get ; set ; }
2347 public string [ ] WatchFileExtensions { get ; set ; }
2448 public ILogger NodeInstanceOutputLogger { get ; set ; }
2549 public bool LaunchWithDebugging { get ; set ; }
2650 public IDictionary < string , string > EnvironmentVariables { get ; set ; }
27- public int ? DebuggingPort { get ; set ; }
51+ public int DebuggingPort { get ; set ; }
2852 }
2953}
0 commit comments