Skip to content

Add support for passing additional arguments to package installation methods #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
var builder = DistributedApplication.CreateBuilder(args);

builder.AddViteApp("vite-demo")
.WithNpmPackageInstallation();
.WithNpmPackageInstallation()
.WithHttpHealthCheck();

builder.AddViteApp("yarn-demo", packageManager: "yarn")
.WithYarnPackageInstallation();
.WithYarnPackageInstallation()
.WithHttpHealthCheck();

builder.AddViteApp("pnpm-demo", packageManager: "pnpm")
.WithPnpmPackageInstallation();
.WithPnpmPackageInstallation()
.WithHttpHealthCheck();

builder.Build().Run();
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,19 @@ public static IResourceBuilder<NodeAppResource> AddPnpmApp(this IDistributedAppl
/// </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>
/// <param name="args">Additional arguments to pass to the npm command.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, bool useCI = false)
public static IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, bool useCI = false, string[]? args = null)
{
// 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);

args ??= [];
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
.WithArgs([useCI ? "ci" : "install"])
.WithArgs([useCI ? "ci" : "install", .. args])
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();

Expand All @@ -135,17 +137,19 @@ public static IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this
/// 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>
/// <param name="args">Additional arguments to pass to the yarn command.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this IResourceBuilder<NodeAppResource> resource)
public static IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this IResourceBuilder<NodeAppResource> resource, string[]? args = null)
{
// 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);

args ??= [];
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
.WithArgs(["install"])
.WithArgs(["install", .. args])
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();

Expand All @@ -160,17 +164,19 @@ public static IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this
/// 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>
/// <param name="args">Additional arguments to pass to the pnpm command.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithPnpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource)
public static IResourceBuilder<NodeAppResource> WithPnpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, string[]? args = null)
{
// 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);

args ??= [];
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
.WithArgs(["install"])
.WithArgs(["install", .. args])
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();

Expand Down
21 changes: 21 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.NodeJS.Extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ builder.AddPnpmApp("pnpm-demo")
.WithExternalHttpEndpoints();
```

### Package installation with custom flags

You can pass additional flags to package managers during installation:

```csharp
// npm with legacy peer deps support
builder.AddNpmApp("npm-app", "./path/to/app")
.WithNpmPackageInstallation(useCI: false, args: ["--legacy-peer-deps"])
.WithExternalHttpEndpoints();

// yarn with frozen lockfile
builder.AddYarnApp("yarn-app", "./path/to/app")
.WithYarnPackageInstallation(args: ["--frozen-lockfile", "--verbose"])
.WithExternalHttpEndpoints();

// pnpm with frozen lockfile
builder.AddPnpmApp("pnpm-app", "./path/to/app")
.WithPnpmPackageInstallation(args: ["--frozen-lockfile"])
.WithExternalHttpEndpoints();
```

## Additional Information

https://learn.microsoft.com/dotnet/aspire/community-toolkit/hosting-nodejs-extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static partial class NodeJSHostingExtensions

public static ApplicationModel.IResourceBuilder<NodeAppResource> AddYarnApp(this IDistributedApplicationBuilder builder, string name, string workingDirectory, string scriptName = "start", string[]? args = null) { throw null; }

public static ApplicationModel.IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this ApplicationModel.IResourceBuilder<NodeAppResource> resource, bool useCI = false) { throw null; }
public static ApplicationModel.IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this ApplicationModel.IResourceBuilder<NodeAppResource> resource, bool useCI = false, string[]? args = null) { throw null; }

public static ApplicationModel.IResourceBuilder<NodeAppResource> WithPnpmPackageInstallation(this ApplicationModel.IResourceBuilder<NodeAppResource> resource) { throw null; }
public static ApplicationModel.IResourceBuilder<NodeAppResource> WithPnpmPackageInstallation(this ApplicationModel.IResourceBuilder<NodeAppResource> resource, string[]? args = null) { throw null; }

public static ApplicationModel.IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this ApplicationModel.IResourceBuilder<NodeAppResource> resource) { throw null; }
public static ApplicationModel.IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this ApplicationModel.IResourceBuilder<NodeAppResource> resource, string[]? args = null) { throw null; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CommunityToolkit.Aspire.Hosting.NodeJS.Extensions.Tests;

#pragma warning disable CTASPIRE001
public class AppHostTests(AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_NodeJS_Extensions_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_NodeJS_Extensions_AppHost>>
{
[Theory]
Expand All @@ -13,7 +12,7 @@ public async Task ResourceStartsAndRespondsOk(string appName)
{
var httpClient = fixture.CreateHttpClient(appName);

await fixture.App.WaitForTextAsync("VITE", appName).WaitAsync(TimeSpan.FromSeconds(30));
await fixture.ResourceNotificationService.WaitForResourceHealthyAsync(appName).WaitAsync(TimeSpan.FromMinutes(1));

var response = await httpClient.GetAsync("/");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Grpc.Core;

namespace CommunityToolkit.Aspire.Hosting.NodeJS.Extensions.Tests;

Expand Down Expand Up @@ -70,4 +68,93 @@ public void WithNpmPackageInstallation_ExcludedFromPublishMode()
// Verify no wait annotations were added
Assert.False(nodeResource.TryGetAnnotationsOfType<WaitAnnotation>(out _));
}

[Fact]
public async Task WithNpmPackageInstallation_CanAcceptAdditionalArgs()
{
var builder = DistributedApplication.CreateBuilder();

var nodeApp = builder.AddNpmApp("test-app", "./test-app");
var nodeAppWithArgs = builder.AddNpmApp("test-app-args", "./test-app-args");

// Test npm install with additional args
nodeApp.WithNpmPackageInstallation(useCI: false, args: ["--legacy-peer-deps"]);
nodeAppWithArgs.WithNpmPackageInstallation(useCI: true, args: ["--verbose", "--no-optional"]);

using var app = builder.Build();

var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var installerResources = appModel.Resources.OfType<NpmInstallerResource>().ToList();

Assert.Equal(2, installerResources.Count);

var installResource = installerResources.Single(r => r.Name == "test-app-npm-install");
var ciResource = installerResources.Single(r => r.Name == "test-app-args-npm-install");

// Verify install command with additional args
var installArgs = await installResource.GetArgumentValuesAsync();
Assert.Collection(
installArgs,
arg => Assert.Equal("install", arg),
arg => Assert.Equal("--legacy-peer-deps", arg)
);

// Verify ci command with additional args
var ciArgs = await ciResource.GetArgumentValuesAsync();
Assert.Collection(
ciArgs,
arg => Assert.Equal("ci", arg),
arg => Assert.Equal("--verbose", arg),
arg => Assert.Equal("--no-optional", arg)
);
}

[Fact]
public async Task WithYarnPackageInstallation_CanAcceptAdditionalArgs()
{
var builder = DistributedApplication.CreateBuilder();

var nodeApp = builder.AddYarnApp("test-yarn-app", "./test-yarn-app");
nodeApp.WithYarnPackageInstallation(args: ["--frozen-lockfile", "--verbose"]);

using var app = builder.Build();

var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var installerResources = appModel.Resources.OfType<YarnInstallerResource>().ToList();

var installerResource = Assert.Single(installerResources);
Assert.Equal("test-yarn-app-yarn-install", installerResource.Name);

var args = await installerResource.GetArgumentValuesAsync();
Assert.Collection(
args,
arg => Assert.Equal("install", arg),
arg => Assert.Equal("--frozen-lockfile", arg),
arg => Assert.Equal("--verbose", arg)
);
}

[Fact]
public async Task WithPnpmPackageInstallation_CanAcceptAdditionalArgs()
{
var builder = DistributedApplication.CreateBuilder();

var nodeApp = builder.AddPnpmApp("test-pnpm-app", "./test-pnpm-app");
nodeApp.WithPnpmPackageInstallation(args: ["--frozen-lockfile"]);

using var app = builder.Build();

var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var installerResources = appModel.Resources.OfType<PnpmInstallerResource>().ToList();

var installerResource = Assert.Single(installerResources);
Assert.Equal("test-pnpm-app-pnpm-install", installerResource.Name);

var args = await installerResource.GetArgumentValuesAsync();
Assert.Collection(
args,
arg => Assert.Equal("install", arg),
arg => Assert.Equal("--frozen-lockfile", arg)
);
}
}
Loading