Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit 7d4ede2

Browse files
authored
Merge pull request #32 from PrinceManfred/master
Optional Task Kill Parameter
2 parents d21fabc + 31f1711 commit 7d4ede2

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/VueCliMiddleware/VueDevelopmentServerMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static class VueCliMiddleware
1818

1919
public static void Attach(
2020
ISpaBuilder spaBuilder,
21-
string scriptName, int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = DefaultRegex)
21+
string scriptName, int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = DefaultRegex, bool forceKill = false)
2222
{
2323
var sourcePath = spaBuilder.Options.SourcePath;
2424
if (string.IsNullOrEmpty(sourcePath))
@@ -34,7 +34,7 @@ public static void Attach(
3434
// Start vue-cli and attach to middleware pipeline
3535
var appBuilder = spaBuilder.ApplicationBuilder;
3636
var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName);
37-
var portTask = StartVueCliServerAsync(sourcePath, scriptName, logger, port, runner, regex);
37+
var portTask = StartVueCliServerAsync(sourcePath, scriptName, logger, port, runner, regex, forceKill);
3838

3939
// Everything we proxy is hardcoded to target http://localhost because:
4040
// - the requests are always from the local machine (we're not accepting remote
@@ -57,7 +57,7 @@ public static void Attach(
5757
}
5858

5959
private static async Task<int> StartVueCliServerAsync(
60-
string sourcePath, string npmScriptName, ILogger logger, int portNumber, ScriptRunnerType runner, string regex)
60+
string sourcePath, string npmScriptName, ILogger logger, int portNumber, ScriptRunnerType runner, string regex, bool forceKill = false)
6161
{
6262
if (portNumber < 80)
6363
{
@@ -67,7 +67,7 @@ private static async Task<int> StartVueCliServerAsync(
6767
{
6868
// if the port we want to use is occupied, terminate the process utilizing that port.
6969
// this occurs when "stop" is used from the debugger and the middleware does not have the opportunity to kill the process
70-
PidUtils.KillPort((ushort)portNumber);
70+
PidUtils.KillPort((ushort)portNumber, forceKill);
7171
}
7272
logger.LogInformation($"Starting server on port {portNumber}...");
7373

src/VueCliMiddleware/VueDevelopmentServerMiddlewareExtensions.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public static void UseVueCli(
2929
string npmScript = "serve",
3030
int port = 8080,
3131
ScriptRunnerType runner = ScriptRunnerType.Npm,
32-
string regex = VueCliMiddleware.DefaultRegex)
32+
string regex = VueCliMiddleware.DefaultRegex,
33+
bool forceKill = false)
3334
{
3435
if (spaBuilder == null)
3536
{
@@ -43,7 +44,7 @@ public static void UseVueCli(
4344
throw new InvalidOperationException($"To use {nameof(UseVueCli)}, you must supply a non-empty value for the {nameof(SpaOptions.SourcePath)} property of {nameof(SpaOptions)} when calling {nameof(SpaApplicationBuilderExtensions.UseSpa)}.");
4445
}
4546

46-
VueCliMiddleware.Attach(spaBuilder, npmScript, port, runner: runner, regex: regex);
47+
VueCliMiddleware.Attach(spaBuilder, npmScript, port, runner: runner, regex: regex, forceKill: forceKill);
4748
}
4849

4950

@@ -54,10 +55,11 @@ public static IEndpointConventionBuilder MapToVueCliProxy(
5455
string npmScript = "serve",
5556
int port = 8080,
5657
ScriptRunnerType runner = ScriptRunnerType.Npm,
57-
string regex = VueCliMiddleware.DefaultRegex)
58+
string regex = VueCliMiddleware.DefaultRegex,
59+
bool forceKill = false)
5860
{
5961
if (pattern == null) { throw new ArgumentNullException(nameof(pattern)); }
60-
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, options, npmScript, port, runner, regex));
62+
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, options, npmScript, port, runner, regex, forceKill));
6163
}
6264

6365
public static IEndpointConventionBuilder MapToVueCliProxy(
@@ -67,11 +69,12 @@ public static IEndpointConventionBuilder MapToVueCliProxy(
6769
string npmScript = "serve",
6870
int port = 8080,
6971
ScriptRunnerType runner = ScriptRunnerType.Npm,
70-
string regex = VueCliMiddleware.DefaultRegex)
72+
string regex = VueCliMiddleware.DefaultRegex,
73+
bool forceKill = false)
7174
{
7275
if (pattern == null) { throw new ArgumentNullException(nameof(pattern)); }
7376
if (sourcePath == null) { throw new ArgumentNullException(nameof(sourcePath)); }
74-
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, runner, regex));
77+
return endpoints.MapFallback(pattern, CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, runner, regex, forceKill));
7578
}
7679

7780
public static IEndpointConventionBuilder MapToVueCliProxy(
@@ -80,9 +83,10 @@ public static IEndpointConventionBuilder MapToVueCliProxy(
8083
string npmScript = "serve",
8184
int port = 8080,
8285
ScriptRunnerType runner = ScriptRunnerType.Npm,
83-
string regex = VueCliMiddleware.DefaultRegex)
86+
string regex = VueCliMiddleware.DefaultRegex,
87+
bool forceKill = false)
8488
{
85-
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, options, npmScript, port, runner, regex));
89+
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, options, npmScript, port, runner, regex, forceKill));
8690
}
8791

8892
public static IEndpointConventionBuilder MapToVueCliProxy(
@@ -91,10 +95,11 @@ public static IEndpointConventionBuilder MapToVueCliProxy(
9195
string npmScript = "serve",
9296
int port = 8080,
9397
ScriptRunnerType runner = ScriptRunnerType.Npm,
94-
string regex = VueCliMiddleware.DefaultRegex)
98+
string regex = VueCliMiddleware.DefaultRegex,
99+
bool forceKill = false)
95100
{
96101
if (sourcePath == null) { throw new ArgumentNullException(nameof(sourcePath)); }
97-
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, runner, regex));
102+
return endpoints.MapFallback("{*path}", CreateProxyRequestDelegate(endpoints, new SpaOptions { SourcePath = sourcePath }, npmScript, port, runner, regex, forceKill));
98103
}
99104

100105

@@ -105,7 +110,8 @@ private static RequestDelegate CreateProxyRequestDelegate(
105110
string npmScript = "serve",
106111
int port = 8080,
107112
ScriptRunnerType runner = ScriptRunnerType.Npm,
108-
string regex = VueCliMiddleware.DefaultRegex)
113+
string regex = VueCliMiddleware.DefaultRegex,
114+
bool forceKill = false)
109115
{
110116
// based on CreateRequestDelegate() https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/StaticFiles/src/StaticFilesEndpointRouteBuilderExtensions.cs#L194
111117

@@ -133,7 +139,7 @@ private static RequestDelegate CreateProxyRequestDelegate(
133139

134140
if (!string.IsNullOrWhiteSpace(npmScript))
135141
{
136-
opt.UseVueCli(npmScript, port, runner, regex);
142+
opt.UseVueCli(npmScript, port, runner, regex, forceKill);
137143
}
138144
});
139145

0 commit comments

Comments
 (0)