private static async Task <AureliaCliMiddleware.AureliaCliServerInfo> StartAureliaCliServerAsync(
            string sourcePath,
            string npmScriptName,
            ILogger logger)
        {
            int availablePort = TcpPortFinder.FindAvailablePort();
            //logger.LogInformation(string.Format("Starting @aurelia cli on port {0}...", (object) availablePort));
            NpmScriptRunner npmScriptRunner =
                new NpmScriptRunner(sourcePath, npmScriptName, string.Format("--port {0}", (object)availablePort), (IDictionary <string, string>)null);
            Match match;

            using (EventedStreamStringReader stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    match = await npmScriptRunner.StdOut.WaitForMatch(new Regex("Project is running at (http\\S+)", RegexOptions.None, AureliaCliMiddleware.RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              "The NPM script '" + npmScriptName + "' exited without indicating that the Aurelia CLI was listening for requests. The error output was: " +
                              stdErrReader.ReadAsString(), (Exception)ex);
                }
            }
            Uri cliServerUri = new Uri(match.Groups[1].Value);
            var serverInfo   = new AureliaCliMiddleware.AureliaCliServerInfo()
            {
                Port = cliServerUri.Port
            };
            await AureliaCliMiddleware.WaitForAureliaCliServerToAcceptRequests(cliServerUri);

            return(serverInfo);
        }
Exemple #2
0
 public static void UseAureliaCliServer(this ISpaBuilder spaBuilder, string npmScript)
 {
     if (spaBuilder == null)
     {
         throw new ArgumentNullException(nameof(spaBuilder));
     }
     if (string.IsNullOrEmpty(spaBuilder.Options.SourcePath))
     {
         throw new InvalidOperationException("To use UseAureliaCliServer, you must supply a non-empty value for the SourcePath property of SpaOptions when calling UseSpa.");
     }
     AureliaCliMiddleware.Attach(spaBuilder, npmScript);
 }
        public static void Attach(ISpaBuilder spaBuilder, string npmScriptName)
        {
            string sourcePath = spaBuilder.Options.SourcePath;

            if (string.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentException("Cannot be null or empty", "sourcePath");
            }
            if (string.IsNullOrEmpty(npmScriptName))
            {
                throw new ArgumentException("Cannot be null or empty", nameof(npmScriptName));
            }
            Task <Uri> targetUriTask = AureliaCliMiddleware.StartAureliaCliServerAsync(sourcePath, npmScriptName, null)
                                       .ContinueWith <Uri>((Func <Task <AureliaCliMiddleware.AureliaCliServerInfo>, Uri>)(task => new UriBuilder("http", "localhost", task.Result.Port).Uri));

            spaBuilder.UseProxyToSpaDevelopmentServer((Func <Task <Uri> >)(() =>
            {
                TimeSpan startupTimeout = spaBuilder.Options.StartupTimeout;
                return(targetUriTask.WithTimeout <Uri>(startupTimeout,
                                                       "The Aurelia CLI process did not start listening for requests " + string.Format("within the timeout period of {0} seconds. ", (object)startupTimeout.Seconds) +
                                                       "Check the log output for error information."));
            }));
        }