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

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

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

            npmScriptRunner.AttachToLogger(logger);

            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    // Although the React 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("App running at", 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);
        }
コード例 #2
0
        private static async Task <int> StartCreateVueAppServerAsync(
            string sourcePath,
            string npmScriptName,
            ILogger logger)
        {
            var portNumber = TcpPortFinder.FindAvailablePort();

            logger.LogInformation($"在 {portNumber} 端口上启动Vue服务...");
            var npmScriptRunner = new NpmScriptRunner(sourcePath, npmScriptName, null, new Dictionary <string, string>
            {
                {
                    "PORT",
                    portNumber.ToString()
                },
                {
                    "BROWSER",
                    "none"
                }
            });

            npmScriptRunner.AttachToLogger(logger);
            using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))
            {
                try
                {
                    var match = await npmScriptRunner.StdOut.WaitForMatch(new Regex(
                                                                              !string.IsNullOrEmpty(CompiledSuccessfullyString)
                            ? CompiledSuccessfullyString
                            : "DONE  Compiled successfully", RegexOptions.None, RegexMatchTimeout));
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidOperationException(
                              "NPM 脚本'" + npmScriptName + "' 已退出,但是Vue服务还没开始侦听请求. 错误输出为: " +
                              stdErrReader.ReadAsString(), ex);
                }
            }

            return(portNumber);
        }