Skip to content

Commit 968e85a

Browse files
committed
Allow the Angular/React-DevServer to run on a given port (#16582)
1 parent d4f7a19 commit 968e85a

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/Middleware/SpaServices.Extensions/ref/Microsoft.AspNetCore.SpaServices.Extensions.netcoreapp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public SpaOptions() { }
4343
public Microsoft.AspNetCore.Builder.StaticFileOptions DefaultPageStaticFileOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
4444
public string PackageManagerCommand { get { throw null; } set { } }
4545
public string SourcePath { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
46+
public int DevServerPort { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
4647
public System.TimeSpan StartupTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
4748
}
4849
}

src/Middleware/SpaServices.Extensions/src/AngularCli/AngularCliMiddleware.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static void Attach(
2727
{
2828
var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand;
2929
var sourcePath = spaBuilder.Options.SourcePath;
30+
var devServerPort = spaBuilder.Options.DevServerPort;
3031
if (string.IsNullOrEmpty(sourcePath))
3132
{
3233
throw new ArgumentException("Cannot be null or empty", nameof(sourcePath));
@@ -40,7 +41,7 @@ public static void Attach(
4041
// Start Angular CLI and attach to middleware pipeline
4142
var appBuilder = spaBuilder.ApplicationBuilder;
4243
var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName);
43-
var angularCliServerInfoTask = StartAngularCliServerAsync(sourcePath, scriptName, pkgManagerCommand, logger);
44+
var angularCliServerInfoTask = StartAngularCliServerAsync(sourcePath, scriptName, pkgManagerCommand, devServerPort, logger);
4445

4546
// Everything we proxy is hardcoded to target http://localhost because:
4647
// - the requests are always from the local machine (we're not accepting remote
@@ -63,9 +64,12 @@ public static void Attach(
6364
}
6465

6566
private static async Task<AngularCliServerInfo> StartAngularCliServerAsync(
66-
string sourcePath, string scriptName, string pkgManagerCommand, ILogger logger)
67+
string sourcePath, string scriptName, string pkgManagerCommand, int portNumber, ILogger logger)
6768
{
68-
var portNumber = TcpPortFinder.FindAvailablePort();
69+
if (portNumber == default(int))
70+
{
71+
portNumber = TcpPortFinder.FindAvailablePort();
72+
}
6973
logger.LogInformation($"Starting @angular/cli on port {portNumber}...");
7074

7175
var scriptRunner = new NodeScriptRunner(

src/Middleware/SpaServices.Extensions/src/ReactDevelopmentServer/ReactDevelopmentServerMiddleware.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void Attach(
2626
{
2727
var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand;
2828
var sourcePath = spaBuilder.Options.SourcePath;
29+
var devServerPort = spaBuilder.Options.DevServerPort;
2930
if (string.IsNullOrEmpty(sourcePath))
3031
{
3132
throw new ArgumentException("Cannot be null or empty", nameof(sourcePath));
@@ -39,7 +40,7 @@ public static void Attach(
3940
// Start create-react-app and attach to middleware pipeline
4041
var appBuilder = spaBuilder.ApplicationBuilder;
4142
var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName);
42-
var portTask = StartCreateReactAppServerAsync(sourcePath, scriptName, pkgManagerCommand, logger);
43+
var portTask = StartCreateReactAppServerAsync(sourcePath, scriptName, pkgManagerCommand, devServerPort, logger);
4344

4445
// Everything we proxy is hardcoded to target http://localhost because:
4546
// - the requests are always from the local machine (we're not accepting remote
@@ -62,9 +63,12 @@ public static void Attach(
6263
}
6364

6465
private static async Task<int> StartCreateReactAppServerAsync(
65-
string sourcePath, string scriptName, string pkgManagerCommand, ILogger logger)
66+
string sourcePath, string scriptName, string pkgManagerCommand, int portNumber, ILogger logger)
6667
{
67-
var portNumber = TcpPortFinder.FindAvailablePort();
68+
if (portNumber == default(int))
69+
{
70+
portNumber = TcpPortFinder.FindAvailablePort();
71+
}
6872
logger.LogInformation($"Starting create-react-app server on port {portNumber}...");
6973

7074
var envVars = new Dictionary<string, string>

src/Middleware/SpaServices.Extensions/src/SpaOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ internal SpaOptions(SpaOptions copyFromOptions)
3333
_packageManagerCommand = copyFromOptions.PackageManagerCommand;
3434
DefaultPageStaticFileOptions = copyFromOptions.DefaultPageStaticFileOptions;
3535
SourcePath = copyFromOptions.SourcePath;
36+
DevServerPort = copyFromOptions.DevServerPort;
3637
}
3738

3839
/// <summary>
@@ -70,6 +71,11 @@ public PathString DefaultPage
7071
/// </summary>
7172
public string SourcePath { get; set; }
7273

74+
/// <summary>
75+
/// Controls wether the development server should be used with a dynamic or fixed port.
76+
/// </summary>
77+
public int DevServerPort { get; set; } = default(int);
78+
7379
/// <summary>
7480
/// Gets or sets the name of the package manager executible, (e.g npm,
7581
/// yarn) to run the SPA.

0 commit comments

Comments
 (0)