Skip to content

Node installer tweaks #740

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 1 commit into from
Jun 26, 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
Expand Up @@ -110,22 +110,23 @@ 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>
/// <param name="configureInstaller">Configure the npm installer resource.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, bool useCI = false, string[]? args = null)
public static IResourceBuilder<NodeAppResource> WithNpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, bool useCI = false, Action<IResourceBuilder<NpmInstallerResource>>? configureInstaller = 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", .. args])
.WithArgs([useCI ? "ci" : "install"])
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();

configureInstaller?.Invoke(installerBuilder);

// Make the parent resource wait for the installer to complete
resource.WaitForCompletion(installerBuilder);
}
Expand All @@ -137,22 +138,23 @@ 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>
/// <param name="configureInstaller">Configure the yarn installer resource.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this IResourceBuilder<NodeAppResource> resource, string[]? args = null)
public static IResourceBuilder<NodeAppResource> WithYarnPackageInstallation(this IResourceBuilder<NodeAppResource> resource, Action<IResourceBuilder<YarnInstallerResource>>? configureInstaller = 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", .. args])
.WithArgs("install")
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();

configureInstaller?.Invoke(installerBuilder);

// Make the parent resource wait for the installer to complete
resource.WaitForCompletion(installerBuilder);
}
Expand All @@ -164,22 +166,23 @@ 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>
/// <param name="configureInstaller">Configure the pnpm installer resource.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NodeAppResource> WithPnpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, string[]? args = null)
public static IResourceBuilder<NodeAppResource> WithPnpmPackageInstallation(this IResourceBuilder<NodeAppResource> resource, Action<IResourceBuilder<PnpmInstallerResource>>? configureInstaller = 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", .. args])
.WithArgs("install")
.WithParentRelationship(resource.Resource)
.ExcludeFromManifest();

configureInstaller?.Invoke(installerBuilder);

// Make the parent resource wait for the installer to complete
resource.WaitForCompletion(installerBuilder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ public async Task WithNpmPackageInstallation_CanAcceptAdditionalArgs()
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"]);
nodeApp.WithNpmPackageInstallation(useCI: false, configureInstaller: installerBuilder =>
{
installerBuilder.WithArgs("--legacy-peer-deps");
});
nodeAppWithArgs.WithNpmPackageInstallation(useCI: true, configureInstaller: installerBuilder =>
{
installerBuilder.WithArgs("--verbose", "--no-optional");
});

using var app = builder.Build();

Expand Down Expand Up @@ -115,7 +121,10 @@ 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"]);
nodeApp.WithYarnPackageInstallation(configureInstaller: installerBuilder =>
{
installerBuilder.WithArgs("--frozen-lockfile", "--verbose");
});

using var app = builder.Build();

Expand All @@ -140,7 +149,10 @@ public async Task WithPnpmPackageInstallation_CanAcceptAdditionalArgs()
var builder = DistributedApplication.CreateBuilder();

var nodeApp = builder.AddPnpmApp("test-pnpm-app", "./test-pnpm-app");
nodeApp.WithPnpmPackageInstallation(args: ["--frozen-lockfile"]);
nodeApp.WithPnpmPackageInstallation(configureInstaller: installerBuilder =>
{
installerBuilder.WithArgs("--frozen-lockfile");
});

using var app = builder.Build();

Expand Down
Loading