コード例 #1
0
        private static async Task <AureliaCliServerInfo> StartAureliaCliServerAsync(
            string sourcePath, string npmScriptName, ILogger logger)
        {
            var portNumber = TcpPortFinder.FindAvailablePort();

            logger.LogInformation($"Starting aurelia-cli on port {portNumber}...");

            var npmScriptRunner = new NpmScriptRunner(
                sourcePath, npmScriptName, $" --port {portNumber}", null);

            npmScriptRunner.AttachToLogger(logger);

            Match openBrowserLine;

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    openBrowserLine = await npmScriptRunner.StdOut.WaitForMatch(
                        new Regex(".*Compiled.*successfully.*", RegexOptions.None, RegexMatchTimeout))
                                      .ConfigureAwait(false);
                }
                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()}", ex);
                }
            }

            var uri        = new Uri($"http://localhost:{portNumber}");//openBrowserLine.Groups[1].Value);
            var serverInfo = new AureliaCliServerInfo {
                Port = uri.Port
            };

            // Even after the Aurelia CLI claims to be listening for requests, there's a short
            // period where it will give an error if you make a request too quickly
            await WaitForAureliaCliServerToAcceptRequests(uri).ConfigureAwait(false);

            return(serverInfo);
        }
コード例 #2
0
        private static async Task <VueCliServerInfo> StartVueCliServerAsync(
            string sourcePath, string npmScriptName, ILogger logger)
        {
            var portNumber = TcpPortFinder.FindAvailablePort();

            logger.LogInformation($"Starting @Vue/cli on port {portNumber}...");

            var npmScriptRunner = new NpmScriptRunner(
                sourcePath, npmScriptName, $"--port {portNumber}", null);

            npmScriptRunner.AttachToLogger(logger);

            Match openBrowserLine;

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    openBrowserLine = await npmScriptRunner.StdOut.WaitForMatch(
                        new Regex("  - Local:   (http\\S+)", RegexOptions.None, RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              $"The NPM script '{npmScriptName}' exited without indicating that the " +
                              $"Vue CLI was listening for requests. The error output was: " +
                              $"{stdErrReader.ReadAsString()}", ex);
                }
            }

            var uri        = new Uri(openBrowserLine.Groups[1].Value);
            var serverInfo = new VueCliServerInfo {
                Port = uri.Port, Host = uri.Host, Scheme = uri.Scheme
            };

            // Even after the Vue CLI claims to be listening for requests, there may be a short
            // period where it will give an error if you make a request too quickly
            await WaitForVueCliServerToAcceptRequests(uri);

            return(serverInfo);
        }
コード例 #3
0
        private static async Task <int> StartVueCliServerAsync(
            string sourcePath, string npmScriptName, ILogger logger, int portNumber)
        {
            if (portNumber < 80)
            {
                portNumber = TcpPortFinder.FindAvailablePort();
            }
            logger.LogInformation($"Starting server on port {portNumber}...");

            var envVars = new Dictionary <string, string>
            {
                { "PORT", portNumber.ToString() },
                { "DEV_SERVER_PORT", portNumber.ToString() }, // vue cli 3 uses --port {number}, included below
                { "BROWSER", "none" },                        // We don't want vue-cli to open its own extra browser window pointing to the internal dev server port
            };
            var npmScriptRunner = new NpmScriptRunner(sourcePath, npmScriptName, $"--port {portNumber}", envVars);

            npmScriptRunner.AttachToLogger(logger);

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    // Although the Vue dev server may eventually tell us the URL it's listening on,
                    // it doesn't do so until it's finished compiling, and even then only if there were
                    // no compiler warnings. So instead of waiting for that, consider it ready as soon
                    // as it starts listening for requests.
                    await npmScriptRunner.StdOut.WaitForMatch(
                        new Regex("running at", RegexOptions.None, RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              $"The NPM script '{npmScriptName}' exited without indicating that the " +
                              $"server was listening for requests. The error output was: " +
                              $"{stdErrReader.ReadAsString()}", ex);
                }
            }

            return(portNumber);
        }
コード例 #4
0
        private static async Task <AngularCliServerInfo> StartAngularCliServerAsync(string sourcePath, string npmScriptName, NLog.ILogger logger)
        {
            var availablePort = TcpPortFinder.FindAvailablePort();

            logger.Info($"Starting @angular/cli on port {(object) availablePort}...");

            var npmScriptRunner = new NpmScriptRunner(
                sourcePath, npmScriptName, $"--port {availablePort}", null);

            npmScriptRunner.AttachToLogger(logger);

            Match openBrowserLine;

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    openBrowserLine = await npmScriptRunner.StdOut.WaitForMatch(
                        new Regex("open your browser on (http\\S+)", RegexOptions.None, RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              $"The NPM script '{npmScriptName}' exited without indicating that the " +
                              $"Angular CLI was listening for requests. The error output was: " +
                              $"{stdErrReader.ReadAsString()}", ex);
                }
            }

            var cliServerUri = new Uri(openBrowserLine.Groups[1].Value);
            var serverInfo   = new AngularCliServerInfo
            {
                Port = cliServerUri.Port
            };

            // Even after the Angular CLI claims to be listening for requests, there's a short
            // period where it will give an error if you make a request too quickly
            await WaitForAngularCliServerToAcceptRequests(cliServerUri);

            return(serverInfo);
        }
コード例 #5
0
        private static async Task <int> StartCreateVueAppServerAsync(
            string sourcePath, string npmExe, string npmScriptName, ILogger logger)
        {
            var portNumber = TcpPortFinder.FindAvailablePort();

            logger.LogInformation($"Starting create-vue-app server on port {portNumber}...");

            var envVars = new Dictionary <string, string>
            {
                { "PORT", portNumber.ToString() },
                { "BROWSER", "none" }, // We don't want create-vue-app to open its own extra browser window pointing to the internal dev server port
            };
            //var arguments = $"--port={portNumber}";
            var npmScriptRunner = new NpmScriptRunner(
                sourcePath, npmExe, npmScriptName, null, envVars);

            npmScriptRunner.AttachToLogger(logger);

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    // 尽管vue dev服务器最终可能会告诉我们它监听的URL,
                    // 在编译完成之前,它不会这样做,即使在没有编译器警告的情况下也不会这样做。
                    // 所以与其等着,还不如在它开始倾听请求时尽快准备好。
                    await npmScriptRunner.StdOut.WaitForMatch(
                        new Regex("App running at", RegexOptions.None, RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              $"The NPM script '{npmScriptName}' exited without indicating that the " +
                              $"create-vue-app server was listening for requests. The error output was: " +
                              $"{stdErrReader.ReadAsString()}", ex);
                }
            }

            return(portNumber);
        }
        private static async Task <int> StartVueDevServerAsync(
            string sourcePath, string npmScriptName, ILogger logger)
        {
            var portNumber = TcpPortFinder.FindAvailablePort();

            logger.LogInformation($"Starting Vue dev server on port {portNumber}...");

            var npmScriptRunner = new NpmScriptRunner(
                sourcePath, npmScriptName, $"--port {portNumber} --host localhost", null);

            npmScriptRunner.AttachToLogger(logger);

            Match startDevelopmentServerLine;

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    // Although the Vue dev server may eventually tell us the URL it's listening on,
                    // it doesn't do so until it's finished compiling, and even then only if there were
                    // no compiler warnings. So instead of waiting for that, consider it ready as soon
                    // as it starts listening for requests.
                    //startDevelopmentServerLine = await npmScriptRunner.StdOut.WaitForMatch(
                    //    new Regex("  - Local:   (http\\S+)", RegexOptions.None, RegexMatchTimeout));
                    startDevelopmentServerLine = await npmScriptRunner.StdOut.WaitForMatch(
                        new Regex("DONE", RegexOptions.None, RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              $"The NPM script '{npmScriptName}' exited without indicating that the " +
                              $"vue development server was listening for requests. The error output was: " +
                              $"{stdErrReader.ReadAsString()}", ex);
                }
            }

            return(portNumber);
        }
コード例 #7
0
        /// <inheritdoc />
        public Task Build(ISpaBuilder spaBuilder)
        {
            var sourcePath = spaBuilder.Options.SourcePath;

            if (string.IsNullOrEmpty(sourcePath))
            {
                throw new InvalidOperationException($"To use {nameof(AngularCliBuilder)}, you must supply a non-empty value for the {nameof(SpaOptions.SourcePath)} property of {nameof(SpaOptions)} when calling {nameof(SpaApplicationBuilderExtensions.UseSpa)}.");
            }

            var logger = LoggerFinder.GetOrCreateLogger(
                spaBuilder.ApplicationBuilder,
                nameof(AngularCliBuilder));
            var npmScriptRunner = new NpmScriptRunner(
                sourcePath,
                _npmScriptName,
                "--watch",
                null);

            npmScriptRunner.AttachToLogger(logger);

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    return(npmScriptRunner.StdOut.WaitForMatch(
                               new Regex("chunk", RegexOptions.None, RegexMatchTimeout),
                               BuildTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              $"The NPM script '{_npmScriptName}' exited without indicating success. " +
                              $"Error output was: {stdErrReader.ReadAsString()}", ex);
                }
            }
        }
コード例 #8
0
        internal Task StartAngularCliBuilderAsync(
            string npmScriptName, string sourcePath, ILogger logger)
        {
            var npmScriptRunner = new NpmScriptRunner(
                sourcePath,
                npmScriptName,
                "--watch");

            npmScriptRunner.AttachToLogger(logger);

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    return(npmScriptRunner.StdOut.WaitForMatch(
                               new Regex("chunk"),
                               TimeoutMilliseconds));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException($"The NPM script '{npmScriptName}' exited without indicating success. Error output was: {stdErrReader.ReadAsString()}", ex);
                }
            }
        }
コード例 #9
0
        private static async Task <VueCliServerInfo> StartVueCliServerAsync(
            string sourcePath, string npmScriptName, ILogger logger)
        {
            var portNumber = 8080;//default port for vue cli: 8080

            if (portNumber < 80)
            {
                portNumber = TcpPortFinder.FindAvailablePort();
            }
            else
            {
                // if the port we want to use is occupied, terminate the process utilizing that port.
                // this occurs when "stop" is used from the debugger and the middleware does not have the opportunity to kill the process
                PidUtils.KillPort((ushort)portNumber);
            }

            logger.LogInformation($"Starting @Vue/cli on port {portNumber}...");

            var envVars = new Dictionary <string, string>
            {
                { "PORT", portNumber.ToString() },
                { "DEV_SERVER_PORT", portNumber.ToString() }, // vue cli 3 uses --port {number}, included below
                { "BROWSER", "none" },                        // We don't want vue-cli to open its own extra browser window pointing to the internal dev server port
            };

            var npmScriptRunner = new NpmScriptRunner(
                sourcePath, npmScriptName, $"--port {portNumber}", envVars);

            AppDomain.CurrentDomain.DomainUnload       += (s, e) => npmScriptRunner?.Kill();
            AppDomain.CurrentDomain.ProcessExit        += (s, e) => npmScriptRunner?.Kill();
            AppDomain.CurrentDomain.UnhandledException += (s, e) => npmScriptRunner?.Kill();
            npmScriptRunner.AttachToLogger(logger);

            Match openBrowserLine;

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    openBrowserLine = await npmScriptRunner.StdOut.WaitForMatch(
                        new Regex("  - Local:   (http\\S+)", RegexOptions.None, RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              $"The NPM script '{npmScriptName}' exited without indicating that the " +
                              $"Vue CLI was listening for requests. The error output was: " +
                              $"{stdErrReader.ReadAsString()}", ex);
                }
            }

            var uri        = new Uri(openBrowserLine.Groups[1].Value);
            var serverInfo = new VueCliServerInfo {
                Port = uri.Port, Host = uri.Host, Scheme = uri.Scheme
            };

            // Even after the Vue CLI claims to be listening for requests, there may be a short
            // period where it will give an error if you make a request too quickly
            await WaitForVueCliServerToAcceptRequests(uri);

            return(serverInfo);
        }
        protected override void RunTool()
        {
            var tool = new NpmScriptRunner(FileSystem, Environment, ProcessRunner, Tools, Log);

            tool.RunScript(Settings);
        }