Пример #1
0
        public static void Main(string[] args)
        {
            if (!CommandLine.Arguments.TryGetOptions(args, true, out string mode, out ushort port, out bool https))
            {
                return;
            }

            if (mode == "kill")
            {
                Console.WriteLine($"Killing process serving port {port}...");
                PidUtils.KillPort(port, true, true);
                return;
            }

            CreateHostBuilder(args).Build().Run();
        }
        public void KillPort_8080_KillsVueServe()
        {
            bool success = PidUtils.KillPort(8080, true, true);

            Assert.IsTrue(success);
        }
Пример #3
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);
        }