public ScriptRunner(string workingDirectory, string scriptName, string arguments, IDictionary <string, string> envVars, ScriptRunnerType runner) { 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)); } Runner = runner; var npmExe = GetExeName(); var completeArguments = $"{GetArgPrefix()}{scriptName} {GetArgSuffix()}{arguments ?? string.Empty}"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // 在 Windows 上, NPM 可执行文件是一个. cmd 文件, 因此无法执行 // 直接 (除了与 Usseshelrex否 = true, 但这是没有好处的, 因为 // 它可以防止捕获 stdio)。因此, 我们需要通过 "cmd/c" 调用它。 npmExe = "cmd"; completeArguments = $"/c npm {completeArguments}"; } var processStartInfo = new ProcessStartInfo(npmExe) { 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; } } var process = LaunchNodeProcess(processStartInfo); StdOut = new EventedStreamReader(process.StandardOutput); StdErr = new EventedStreamReader(process.StandardError); }
public EventedStreamStringReader(EventedStreamReader eventedStreamReader) { _eventedStreamReader = eventedStreamReader ?? throw new ArgumentNullException(nameof(eventedStreamReader)); _eventedStreamReader.OnReceivedLine += OnReceivedLine; }