-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathNodeJSHostingExtensions.cs
More file actions
188 lines (162 loc) · 9.67 KB
/
NodeJSHostingExtensions.cs
File metadata and controls
188 lines (162 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using Aspire.Hosting.ApplicationModel;
using CommunityToolkit.Aspire.Utils;
using Microsoft.Extensions.Hosting;
namespace Aspire.Hosting;
/// <summary>
/// Provides extension methods for adding Node.js applications to the distributed application builder.
/// </summary>
public static class NodeJSHostingExtensions
{
/// <summary>
/// Adds a Vite app to the distributed application builder.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/> to add the resource to.</param>
/// <param name="name">The name of the Vite app.</param>
/// <param name="workingDirectory">The working directory of the Vite app. If not specified, it will be set to a path that is a sibling of the AppHost directory using the <paramref name="name"/> as the folder.</param>
/// <param name="packageManager">The package manager to use. Default is npm.</param>
/// <param name="useHttps">When true use HTTPS for the endpoints, otherwise use HTTP.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
/// <remarks>This uses the specified package manager (default npm) method internally but sets defaults that would be expected to run a Vite app, such as the command to run the dev server and exposing the HTTP endpoints.</remarks>
public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string? workingDirectory = null, string packageManager = "npm", bool useHttps = false)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(packageManager);
string wd = workingDirectory ?? Path.Combine("..", name);
var resource = packageManager switch
{
"yarn" => builder.AddYarnApp(name, wd, "dev"),
"pnpm" => builder.AddPnpmApp(name, wd, "dev"),
_ => builder.AddNpmApp(name, wd, "dev")
};
_ = useHttps
? resource.WithHttpsEndpoint(env: "PORT")
: resource.WithHttpEndpoint(env: "PORT");
return resource.WithArgs(ctx =>
{
if (packageManager == "npm")
{
ctx.Args.Add("--");
}
var targetEndpoint = resource.Resource.GetEndpoint(useHttps ? "https" : "http");
ctx.Args.Add("--port");
ctx.Args.Add(targetEndpoint.Property(EndpointProperty.TargetPort));
});
}
/// <summary>
/// Adds a Node.js app to the distributed application builder using yarn as the package manager.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/> to add the resource to.</param>
/// <param name="name">The name of the resource.</param>
/// <param name="workingDirectory">The working directory to use for the command. If null, the working directory of the current process is used.</param>
/// <param name="scriptName">The npm script to execute. Defaults to "start".</param>
/// <param name="args">The arguments to pass to the command.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> AddYarnApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string scriptName = "start", string[]? args = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(workingDirectory);
ArgumentNullException.ThrowIfNull(scriptName);
string[] allArgs = args is { Length: > 0 }
? ["run", scriptName, "--", .. args]
: ["run", scriptName];
workingDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, workingDirectory));
var resource = new NodeAppResource(name, "yarn", workingDirectory);
return builder.AddResource(resource)
.WithNodeDefaults()
.WithArgs(allArgs);
}
/// <summary>
/// Adds a Node.js app to the distributed application builder using pnpm as the package manager.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/> to add the resource to.</param>
/// <param name="name">The name of the resource.</param>
/// <param name="workingDirectory">The working directory to use for the command. If null, the working directory of the current process is used.</param>
/// <param name="scriptName">The npm script to execute. Defaults to "start".</param>
/// <param name="args">The arguments to pass to the command.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> AddPnpmApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string scriptName = "start", string[]? args = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(workingDirectory);
ArgumentNullException.ThrowIfNull(scriptName);
string[] allArgs = args is { Length: > 0 }
? ["run", scriptName, "--", .. args]
: ["run", scriptName];
workingDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, workingDirectory));
var resource = new NodeAppResource(name, "pnpm", workingDirectory);
return builder.AddResource(resource)
.WithNodeDefaults()
.WithArgs(allArgs);
}
/// <summary>
/// Ensures the Node.js packages are installed before the application starts using npm as the package manager.
/// </summary>
/// <param name="resource">The Node.js app resource.</param>
/// <param name="useCI">When true use <code>npm ci</code> otherwise use <code>npm install</code> when installing packages.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, bool useCI = false)
{
// Only install packages during development, not in publish mode
if (!resource.ApplicationBuilder.ExecutionContext.IsPublishMode)
{
var installerName = $"{resource.Resource.Name}-npm-install";
var installer = new NpmInstallerResource(installerName, resource.Resource.WorkingDirectory);
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
.WithArgs([useCI ? "ci" : "install"])
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();
// Make the parent resource wait for the installer to complete
resource.WaitForCompletion(installerBuilder);
}
return resource;
}
/// <summary>
/// Ensures the Node.js packages are installed before the application starts using yarn as the package manager.
/// </summary>
/// <param name="resource">The Node.js app resource.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this IResourceBuilder<NodeAppResource> resource)
{
// Only install packages during development, not in publish mode
if (!resource.ApplicationBuilder.ExecutionContext.IsPublishMode)
{
var installerName = $"{resource.Resource.Name}-yarn-install";
var installer = new YarnInstallerResource(installerName, resource.Resource.WorkingDirectory);
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
.WithArgs(["install"])
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();
// Make the parent resource wait for the installer to complete
resource.WaitForCompletion(installerBuilder);
}
return resource;
}
/// <summary>
/// Ensures the Node.js packages are installed before the application starts using pnpm as the package manager.
/// </summary>
/// <param name="resource">The Node.js app resource.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithPnpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource)
{
// Only install packages during development, not in publish mode
if (!resource.ApplicationBuilder.ExecutionContext.IsPublishMode)
{
var installerName = $"{resource.Resource.Name}-pnpm-install";
var installer = new PnpmInstallerResource(installerName, resource.Resource.WorkingDirectory);
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
.WithArgs(["install"])
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();
// Make the parent resource wait for the installer to complete
resource.WaitForCompletion(installerBuilder);
}
return resource;
}
// Copied from https://github.com/dotnet/aspire/blob/50ca9fa670af5c70782dc75d2961956b06f1a403/src/Aspire.Hosting.NodeJs/NodeExtensions.cs#L70-L72
private static IResourceBuilder<NodeAppResource> WithNodeDefaults(this IResourceBuilder<NodeAppResource> builder) =>
builder.WithOtlpExporter()
.WithEnvironment("NODE_ENV", builder.ApplicationBuilder.Environment.IsDevelopment() ? "development" : "production");
}