public ScriptRunner(string workingDirectory, string scriptName, string arguments, IDictionary <string, string> envVars, ScriptRunnerType runner)
        {
            _scriptName = scriptName;
            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 exeName           = GetExeName();
            var completeArguments = BuildCommand(runner, scriptName, arguments);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // On Windows, the NPM 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".
                completeArguments = $"/c {exeName} {completeArguments}";
                exeName           = "cmd";
            }

            var processStartInfo = new ProcessStartInfo(exeName)
            {
                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;
                }
            }

            RunnerProcess = LaunchNodeProcess(processStartInfo);

            StdOut = new EventedStreamReader(RunnerProcess.StandardOutput);
            StdErr = new EventedStreamReader(RunnerProcess.StandardError);
        }
Example #2
0
 public EventedStreamStringReader(EventedStreamReader eventedStreamReader)
 {
     _eventedStreamReader = eventedStreamReader
                            ?? throw new ArgumentNullException(nameof(eventedStreamReader));
     _eventedStreamReader.OnReceivedLine += OnReceivedLine;
 }