Skip to content

Commit

Permalink
Net8 (#115)
Browse files Browse the repository at this point in the history
* net8

* Add check for valid nodejs project
  • Loading branch information
kolesnikovav authored Dec 29, 2023
1 parent fbe7985 commit 87bef44
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/samples/webapi/bin/Debug/net7.0/webapi.dll",
"program": "${workspaceFolder}/samples/webapi/bin/Debug/net8.0/webapi.dll",
"args": [],
"cwd": "${workspaceFolder}",
"justMyCode":false,
Expand Down Expand Up @@ -38,7 +38,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/samples/ConsoleApp/bin/Debug/net7.0/ConsoleApp.dll",
"program": "${workspaceFolder}/samples/ConsoleApp/bin/Debug/net8.0/ConsoleApp.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
Expand Down
4 changes: 2 additions & 2 deletions samples/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion samples/webapi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApp
var p = Path.Combine(wd,"samples", "hello-vite");
app.UseSpa(
spa => {
spa.UseAspSpaDevelopmentServer(hostApplicationLifetime, "yarn", "dev", p, new Dictionary<string,string>(), TimeSpan.FromSeconds(15), null, true,true);
spa.UseAspSpaDevelopmentServer(hostApplicationLifetime, "yarn", "dev", p, [], TimeSpan.FromSeconds(15), null, true,true);
}
);
// this block starts nuxt spa application
Expand Down
2 changes: 1 addition & 1 deletion samples/webapi/webapi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</ItemGroup>

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
10 changes: 5 additions & 5 deletions src/AspSpaService/AspSpaService.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<PackageId>AspSpaService</PackageId>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<Authors>Aleksandr Kolesnikov</Authors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>AspSpaService</AssemblyName>
Expand All @@ -16,13 +16,13 @@
<Description>The Asp Net Core plugin for integrating SPA application with Asp Net Core. This plugin can be used with any web framework in same manner.</Description>
<Keywords>ASP;Asp Net Core; SPA; vue; vite; nuxt; react; svelte</Keywords>
<PackageReadmeFile>README.md</PackageReadmeFile>
<LangVersion>11.0</LangVersion>
<LangVersion>12.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.1.32" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="7.0.9" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<None Include="README.md" Pack="true" PackagePath="\"/>
</ItemGroup>

Expand Down
19 changes: 15 additions & 4 deletions src/AspSpaService/AspSpaServiceMiddlewareExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.AspNetCore.SpaServices;
using Microsoft.Extensions.Hosting;
using System.IO;

namespace AspSpaService;

Expand All @@ -29,6 +30,17 @@ private static NodeRunner GetNodeRunner(IApplicationBuilder builder)
{
return (NodeRunner)builder.ApplicationServices.GetService(typeof(NodeRunner));
}
private static void CheckValidNodeJSProjectFolder(string folderPath)
{
if (!Directory.Exists(folderPath))
{
throw new ArgumentException("Folder :" + folderPath + " does not exists, or unaccessible");
}
if (Directory.GetFiles(folderPath,"package.json",SearchOption.TopDirectoryOnly).Length == 0)
{
throw new ArgumentException("There is no file 'package.json' in " + folderPath);
}
}
/// <summary>
/// Handles requests by passing them through to an instance of the node dev server.
/// This means you don't need to start node dev server manually.
Expand Down Expand Up @@ -60,6 +72,7 @@ public static void UseAspSpaDevelopmentServer(
{
throw new ArgumentNullException(nameof(spaBuilder));
}
CheckValidNodeJSProjectFolder(workingDirectory);
var logger = GetOrCreateLogger(spaBuilder.ApplicationBuilder, LogCategoryName);
NodeRunner runner = GetNodeRunner(spaBuilder.ApplicationBuilder);
runner ??= new NodeRunner();
Expand Down Expand Up @@ -116,10 +129,8 @@ public static void UseAspSpaDevelopmentServer(
bool logError = false,
bool unsubscribeWhenReady = true)
{
if (spaBuilder == null)
{
throw new ArgumentNullException(nameof(spaBuilder));
}
ArgumentNullException.ThrowIfNull(spaBuilder);
CheckValidNodeJSProjectFolder(workingDirectory);
var logger = GetOrCreateLogger(spaBuilder.ApplicationBuilder, LogCategoryName);
NodeRunner runner = GetNodeRunner(spaBuilder.ApplicationBuilder);
runner ??= new NodeRunner();
Expand Down
13 changes: 9 additions & 4 deletions src/AspSpaService/NodeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace AspSpaService;
/// <summary>
/// Node JS Process starter
/// </summary>
public class NodeRunner : IDisposable
public partial class NodeRunner : IDisposable
{
private Process _nodeProcess;
private Uri _uri;
private readonly Regex _regexUri = new(@"(http|https):\/\/(localhost|127\.0\.0\.1):[0-9]+");
private readonly Regex _regexUri = RegexUri();

private readonly Regex _regexSpecial = new(@"\[[0-9]+m");
private readonly Regex _regexSpecial = RegexSpecial();
private readonly EventWaitHandle _awaiter = new(false, EventResetMode.AutoReset);
private NodeStreamReader streamOutputReader;
private NodeStreamReader streamErrorReader;
Expand All @@ -43,7 +43,7 @@ public class NodeRunner : IDisposable
/// <summary>
/// Environment variables for node process
/// </summary>
public Dictionary<string, string> EnvVars { get; set; } = new Dictionary<string, string>();
public Dictionary<string, string> EnvVars { get; set; } = [];
/// <summary>
/// Log Node JS Process messages
/// </summary>
Expand Down Expand Up @@ -194,5 +194,10 @@ public void Dispose()
streamOutputReader?.Dispose();
streamErrorReader?.Dispose();
}

[GeneratedRegex(@"(http|https):\/\/(localhost|127\.0\.0\.1):[0-9]+")]
private static partial Regex RegexUri();
[GeneratedRegex(@"\[[0-9]+m")]
private static partial Regex RegexSpecial();
}

0 comments on commit 87bef44

Please sign in to comment.