-
Notifications
You must be signed in to change notification settings - Fork 795
/
NodeScriptRunner.cs
188 lines (165 loc) · 7.24 KB
/
NodeScriptRunner.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.Logging;
using Serenity.Web.SpaServices;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace Serenity.Web;
/// <summary>
/// Executes the <c>script</c> entries defined in a <c>package.json</c> file,
/// capturing any output written to stdio.
/// </summary>
public sealed class NodeScriptRunner : IDisposable
{
private Process npmProcess;
private EventedStreamReader StdOut { get; }
private EventedStreamReader StdErr { get; }
private static readonly Regex AnsiColorRegex = new("\x001b\\[[0-9;]*m", RegexOptions.None, TimeSpan.FromSeconds(1));
/// <summary>
/// Creates a new instance of the class
/// </summary>
/// <param name="scriptName">Script name</param>
/// <param name="arguments">Arguments</param>
/// <param name="workingDirectory">Working directory</param>
/// <param name="envVars">Environment variables</param>
/// <param name="pkgManagerCommand">Package manager command. Defaul is "npm"</param>
/// <param name="diagnosticSource">Diagnostics source</param>
/// <param name="applicationStoppingToken">Application stopping token</param>
/// <exception cref="ArgumentException">One of arguments is null</exception>
public NodeScriptRunner(string scriptName,
string arguments = null, string workingDirectory = null,
IDictionary<string, string> envVars = null, string pkgManagerCommand = "npm",
DiagnosticSource diagnosticSource = null, CancellationToken applicationStoppingToken = default)
{
if (string.IsNullOrEmpty(workingDirectory))
{
throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
}
if (string.IsNullOrEmpty(scriptName))
{
throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
}
if (string.IsNullOrEmpty(pkgManagerCommand))
{
throw new ArgumentException("Cannot be null or empty.", nameof(pkgManagerCommand));
}
var exeToRun = pkgManagerCommand;
var completeArguments = $"run {scriptName} -- {arguments ?? string.Empty}";
if (OperatingSystem.IsWindows())
{
// On Windows, the node executable is a .cmd file, so it can't be executed
// directly (except with UseShellExecute=true, but that's no good, because
// it prevents capturing stdio). So we need to invoke it via "cmd /c".
exeToRun = "cmd";
completeArguments = $"/c {pkgManagerCommand} {completeArguments}";
}
var processStartInfo = new ProcessStartInfo(exeToRun)
{
Arguments = completeArguments,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = workingDirectory
};
if (envVars != null)
{
foreach (var keyValuePair in envVars)
{
processStartInfo.Environment[keyValuePair.Key] = keyValuePair.Value;
}
}
npmProcess = LaunchNodeProcess(processStartInfo, pkgManagerCommand);
StdOut = new EventedStreamReader(npmProcess.StandardOutput);
StdErr = new EventedStreamReader(npmProcess.StandardError);
applicationStoppingToken.Register(((IDisposable)this).Dispose);
if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.NodeServices.Npm.NpmStarted"))
{
WriteDiagnosticEvent(
diagnosticSource,
"Microsoft.AspNetCore.NodeServices.Npm.NpmStarted",
new
{
processStartInfo,
process = npmProcess
});
}
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
Justification = "The values being passed into Write have the commonly used properties being preserved with DynamicDependency.")]
static void WriteDiagnosticEvent<TValue>(DiagnosticSource diagnosticSource, string name, TValue value)
=> diagnosticSource.Write(name, value);
}
/// <summary>
/// Attaches to the logger
/// </summary>
/// <param name="logger">Logger</param>
public void AttachToLogger(ILogger logger)
{
#pragma warning disable CA2254 // Template should be a static expression
// When the node task emits complete lines, pass them through to the real logger
StdOut.OnReceivedLine += line =>
{
if (!string.IsNullOrWhiteSpace(line))
{
// Node tasks commonly emit ANSI colors, but it wouldn't make sense to forward
// those to loggers (because a logger isn't necessarily any kind of terminal)
logger.LogInformation(StripAnsiColors(line));
}
};
StdErr.OnReceivedLine += line =>
{
if (!string.IsNullOrWhiteSpace(line))
{
// workaround for esbuild as it logs to stderr
if (line.StartsWith("[watch]"))
logger.LogInformation(StripAnsiColors(line));
else
logger.LogError(StripAnsiColors(line));
}
};
#pragma warning restore CA2254 // Template should be a static expression
/*
// But when it emits incomplete lines, assume this is progress information and
// hence just pass it through to StdOut regardless of logger config.
StdErr.OnReceivedChunk += chunk =>
{
Debug.Assert(chunk.Array != null);
var containsNewline = Array.IndexOf(
chunk.Array, '\n', chunk.Offset, chunk.Count) >= 0;
if (!containsNewline)
{
Console.Write(chunk.Array, chunk.Offset, chunk.Count);
}
};*/
}
private static string StripAnsiColors(string line)
=> AnsiColorRegex.Replace(line, string.Empty);
private static Process LaunchNodeProcess(ProcessStartInfo startInfo, string commandName)
{
try
{
var process = Process.Start(startInfo)!;
// See equivalent comment in OutOfProcessNodeInstance.cs for why
process.EnableRaisingEvents = true;
return process;
}
catch (Exception ex)
{
var message = $"Failed to start '{commandName}'. To resolve this:.\n\n"
+ $"[1] Ensure that '{commandName}' is installed and can be found in one of the PATH directories.\n"
+ $" Current PATH enviroment variable is: {Environment.GetEnvironmentVariable("PATH")}\n"
+ " Make sure the executable is in one of those directories, or update your PATH.\n\n"
+ "[2] See the InnerException for further details of the cause.";
throw new InvalidOperationException(message, ex);
}
}
void IDisposable.Dispose()
{
if (npmProcess != null && !npmProcess.HasExited)
{
npmProcess.Kill(entireProcessTree: true);
npmProcess = null;
}
}
}