Skip to content

Commit d5bc63d

Browse files
committed
handling Windows invocation of npm and associated commands
1 parent d0c44b0 commit d5bc63d

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

src/Aspire.CommunityToolkit.Hosting.NodeJS.Extensions/NodePackageInstaller.cs

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ await notificationService.PublishUpdateAsync(resource, state => state with
6060

6161
logger.LogInformation("Installing {PackageManager} packages in {WorkingDirectory}", packageManager, resource.WorkingDirectory);
6262

63-
var packageInstaller = new Process
63+
var packageInstaller = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? => new Process
6464
{
6565
StartInfo = new ProcessStartInfo
6666
{
@@ -69,51 +69,64 @@ await notificationService.PublishUpdateAsync(resource, state => state with
6969
WorkingDirectory = resource.WorkingDirectory,
7070
RedirectStandardOutput = true,
7171
RedirectStandardError = true,
72-
UseShellExecute = false,
73-
CreateNoWindow = true
72+
CreateNoWindow = true,
73+
UseShellExecute = true,
7474
}
75-
};
76-
77-
packageInstaller.OutputDataReceived += async (sender, args) =>
78-
{
79-
if (!string.IsNullOrWhiteSpace(args.Data))
75+
: new Process
8076
{
81-
await notificationService.PublishUpdateAsync(resource, state => state with
77+
StartInfo = new ProcessStartInfo
8278
{
83-
State = new(args.Data, KnownResourceStates.Starting)
84-
}).ConfigureAwait(false);
85-
86-
logger.LogInformation("{Data}", args.Data);
79+
FileName = "cmd",
80+
Arguments = $"/c {packageManager} install",
81+
WorkingDirectory = resource.WorkingDirectory,
82+
RedirectStandardOutput = true,
83+
RedirectStandardError = true,
84+
CreateNoWindow = true,
85+
UseShellExecute = false,
86+
}
8787
}
8888
};
8989

90-
packageInstaller.ErrorDataReceived += async (sender, args) =>
90+
packageInstaller.OutputDataReceived += async(sender, args) =>
9191
{
9292
if (!string.IsNullOrWhiteSpace(args.Data))
9393
{
9494
await notificationService.PublishUpdateAsync(resource, state => state with
9595
{
96-
State = new(args.Data, KnownResourceStates.FailedToStart)
96+
State = new(args.Data, KnownResourceStates.Starting)
9797
}).ConfigureAwait(false);
9898

99-
logger.LogError("{Data}", args.Data);
99+
logger.LogInformation("{Data}", args.Data);
100100
}
101101
};
102102

103-
packageInstaller.Start();
104-
packageInstaller.BeginOutputReadLine();
105-
packageInstaller.BeginErrorReadLine();
103+
packageInstaller.ErrorDataReceived += async (sender, args) =>
104+
{
105+
if (!string.IsNullOrWhiteSpace(args.Data))
106+
{
107+
await notificationService.PublishUpdateAsync(resource, state => state with
108+
{
109+
State = new(args.Data, KnownResourceStates.FailedToStart)
110+
}).ConfigureAwait(false);
111+
112+
logger.LogError("{Data}", args.Data);
113+
}
114+
};
106115

107-
packageInstaller.WaitForExit();
116+
packageInstaller.Start();
117+
packageInstaller.BeginOutputReadLine();
118+
packageInstaller.BeginErrorReadLine();
108119

109-
if (packageInstaller.ExitCode != 0)
110-
{
111-
await notificationService.PublishUpdateAsync(resource, state => state with
112-
{
113-
State = new($"{packageManager} exited with {packageInstaller.ExitCode}", KnownResourceStates.FailedToStart)
114-
}).ConfigureAwait(false);
120+
packageInstaller.WaitForExit();
115121

116-
throw new InvalidOperationException($"{packageManager} install failed with exit code {packageInstaller.ExitCode}");
117-
}
122+
if (packageInstaller.ExitCode != 0)
123+
{
124+
await notificationService.PublishUpdateAsync(resource, state => state with
125+
{
126+
State = new($"{packageManager} exited with {packageInstaller.ExitCode}", KnownResourceStates.FailedToStart)
127+
}).ConfigureAwait(false);
128+
129+
throw new InvalidOperationException($"{packageManager} install failed with exit code {packageInstaller.ExitCode}");
130+
}
118131
}
119132
}

0 commit comments

Comments
 (0)