Merge pull request #16599 from flensrocker/issue-16582

Allow the Angular/React-DevServer to run on a given port (#16582)
This commit is contained in:
Ryan Brandenburg 2019-11-06 13:43:23 -08:00 committed by GitHub
commit 7202634cea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 6 deletions

View File

@ -41,6 +41,7 @@ namespace Microsoft.AspNetCore.SpaServices
public SpaOptions() { } public SpaOptions() { }
public Microsoft.AspNetCore.Http.PathString DefaultPage { get { throw null; } set { } } public Microsoft.AspNetCore.Http.PathString DefaultPage { get { throw null; } set { } }
public Microsoft.AspNetCore.Builder.StaticFileOptions DefaultPageStaticFileOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public Microsoft.AspNetCore.Builder.StaticFileOptions DefaultPageStaticFileOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
public int DevServerPort { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
public string PackageManagerCommand { get { throw null; } set { } } public string PackageManagerCommand { get { throw null; } set { } }
public string SourcePath { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public string SourcePath { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
public System.TimeSpan StartupTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public System.TimeSpan StartupTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }

View File

@ -27,6 +27,7 @@ namespace Microsoft.AspNetCore.SpaServices.AngularCli
{ {
var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand; var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand;
var sourcePath = spaBuilder.Options.SourcePath; var sourcePath = spaBuilder.Options.SourcePath;
var devServerPort = spaBuilder.Options.DevServerPort;
if (string.IsNullOrEmpty(sourcePath)) if (string.IsNullOrEmpty(sourcePath))
{ {
throw new ArgumentException("Cannot be null or empty", nameof(sourcePath)); throw new ArgumentException("Cannot be null or empty", nameof(sourcePath));
@ -40,7 +41,7 @@ namespace Microsoft.AspNetCore.SpaServices.AngularCli
// Start Angular CLI and attach to middleware pipeline // Start Angular CLI and attach to middleware pipeline
var appBuilder = spaBuilder.ApplicationBuilder; var appBuilder = spaBuilder.ApplicationBuilder;
var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName); var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName);
var angularCliServerInfoTask = StartAngularCliServerAsync(sourcePath, scriptName, pkgManagerCommand, logger); var angularCliServerInfoTask = StartAngularCliServerAsync(sourcePath, scriptName, pkgManagerCommand, devServerPort, logger);
// Everything we proxy is hardcoded to target http://localhost because: // Everything we proxy is hardcoded to target http://localhost because:
// - the requests are always from the local machine (we're not accepting remote // - the requests are always from the local machine (we're not accepting remote
@ -63,9 +64,12 @@ namespace Microsoft.AspNetCore.SpaServices.AngularCli
} }
private static async Task<AngularCliServerInfo> StartAngularCliServerAsync( private static async Task<AngularCliServerInfo> StartAngularCliServerAsync(
string sourcePath, string scriptName, string pkgManagerCommand, ILogger logger) string sourcePath, string scriptName, string pkgManagerCommand, int portNumber, ILogger logger)
{ {
var portNumber = TcpPortFinder.FindAvailablePort(); if (portNumber == default(int))
{
portNumber = TcpPortFinder.FindAvailablePort();
}
logger.LogInformation($"Starting @angular/cli on port {portNumber}..."); logger.LogInformation($"Starting @angular/cli on port {portNumber}...");
var scriptRunner = new NodeScriptRunner( var scriptRunner = new NodeScriptRunner(

View File

@ -26,6 +26,7 @@ namespace Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer
{ {
var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand; var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand;
var sourcePath = spaBuilder.Options.SourcePath; var sourcePath = spaBuilder.Options.SourcePath;
var devServerPort = spaBuilder.Options.DevServerPort;
if (string.IsNullOrEmpty(sourcePath)) if (string.IsNullOrEmpty(sourcePath))
{ {
throw new ArgumentException("Cannot be null or empty", nameof(sourcePath)); throw new ArgumentException("Cannot be null or empty", nameof(sourcePath));
@ -39,7 +40,7 @@ namespace Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer
// Start create-react-app and attach to middleware pipeline // Start create-react-app and attach to middleware pipeline
var appBuilder = spaBuilder.ApplicationBuilder; var appBuilder = spaBuilder.ApplicationBuilder;
var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName); var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName);
var portTask = StartCreateReactAppServerAsync(sourcePath, scriptName, pkgManagerCommand, logger); var portTask = StartCreateReactAppServerAsync(sourcePath, scriptName, pkgManagerCommand, devServerPort, logger);
// Everything we proxy is hardcoded to target http://localhost because: // Everything we proxy is hardcoded to target http://localhost because:
// - the requests are always from the local machine (we're not accepting remote // - the requests are always from the local machine (we're not accepting remote
@ -62,9 +63,12 @@ namespace Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer
} }
private static async Task<int> StartCreateReactAppServerAsync( private static async Task<int> StartCreateReactAppServerAsync(
string sourcePath, string scriptName, string pkgManagerCommand, ILogger logger) string sourcePath, string scriptName, string pkgManagerCommand, int portNumber, ILogger logger)
{ {
var portNumber = TcpPortFinder.FindAvailablePort(); if (portNumber == default(int))
{
portNumber = TcpPortFinder.FindAvailablePort();
}
logger.LogInformation($"Starting create-react-app server on port {portNumber}..."); logger.LogInformation($"Starting create-react-app server on port {portNumber}...");
var envVars = new Dictionary<string, string> var envVars = new Dictionary<string, string>

View File

@ -33,6 +33,7 @@ namespace Microsoft.AspNetCore.SpaServices
_packageManagerCommand = copyFromOptions.PackageManagerCommand; _packageManagerCommand = copyFromOptions.PackageManagerCommand;
DefaultPageStaticFileOptions = copyFromOptions.DefaultPageStaticFileOptions; DefaultPageStaticFileOptions = copyFromOptions.DefaultPageStaticFileOptions;
SourcePath = copyFromOptions.SourcePath; SourcePath = copyFromOptions.SourcePath;
DevServerPort = copyFromOptions.DevServerPort;
} }
/// <summary> /// <summary>
@ -70,6 +71,11 @@ namespace Microsoft.AspNetCore.SpaServices
/// </summary> /// </summary>
public string SourcePath { get; set; } public string SourcePath { get; set; }
/// <summary>
/// Controls wether the development server should be used with a dynamic or fixed port.
/// </summary>
public int DevServerPort { get; set; } = default(int);
/// <summary> /// <summary>
/// Gets or sets the name of the package manager executible, (e.g npm, /// Gets or sets the name of the package manager executible, (e.g npm,
/// yarn) to run the SPA. /// yarn) to run the SPA.