Esempio n. 1
0
        public bool Run(Item item, OutputFormat format, SiteInfo siteInfo, out LighthouseJson lighthouseJson)
        {
            lighthouseJson = null;
            var processOutput = new ConcurrentBag <string>();

            _paths.CreateReportDir(item);

            var process   = new System.Diagnostics.Process();
            var startInfo = new System.Diagnostics.ProcessStartInfo();

            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName    = _paths.GetLighthouseCmdPath();
            startInfo.Arguments  += $"{_urls.GetItemUrl(item, siteInfo)}";
            var path = "";

            switch (format)
            {
            case OutputFormat.Html:
                path = _paths.GetReportHtmlPath(item);
                break;

            case OutputFormat.Json:
                path = _paths.GetReportJsonPath(item);
                startInfo.Arguments += GetOutputFormat(format);
                break;
            }

            startInfo.Arguments += GetOutputPath(path);
            startInfo.Arguments += " --chrome-flags=\"--ignore-certificate-errors\"";

            startInfo.UseShellExecute        = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError  = true;
            process.StartInfo           = startInfo;
            process.OutputDataReceived += (sender, eventArgs) => processOutput.Add(eventArgs.Data);
            process.ErrorDataReceived  += (sender, eventArgs) => processOutput.Add(eventArgs.Data);

            Sitecore.Diagnostics.Log.Error(startInfo.FileName, this);
            Sitecore.Diagnostics.Log.Error(startInfo.Arguments, this);
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit(Settings.ProcessTimeout);
            if (process.HasExited)
            {
                if (format == OutputFormat.Json)
                {
                    var json  = File.ReadAllText(path);
                    var model = JsonConvert.DeserializeObject <LighthouseJson>(json);
                    _sitecoreData.AddCheckPoint(item, model);
                    lighthouseJson = model;
                }
                return(true);
            }
            return(false);
        }