Esempio n. 1
0
        public NodeScriptRunner(
            string workingDirectory,
            string scriptName,
            string arguments,
            IDictionary <string, string> envVars,
            string pkgManagerCommand,
            DiagnosticSource diagnosticSource,
            CancellationToken applicationStoppingToken)
        {
            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 (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // 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"))
            {
                diagnosticSource.Write(
                    "Microsoft.AspNetCore.NodeServices.Npm.NpmStarted",
                    new
                {
                    processStartInfo = processStartInfo,
                    process          = _npmProcess
                });
            }
        }
 public EventedStreamStringReader(EventedStreamReader eventedStreamReader)
 {
     _eventedStreamReader = eventedStreamReader
                            ?? throw new ArgumentNullException(nameof(eventedStreamReader));
     _eventedStreamReader.OnReceivedLine += OnReceivedLine;
 }