Beispiel #1
0
        private void CheckPreRequesites()
        {
            if (!NodeJSUtil.IsNodeJsInstalled())
            {
                MessageBox.Show("Node JS is not installed, please install it.\nExiting...", "Node JS Not Found");
                Environment.Exit(1);
            }

            if (!NodeJSUtil.IsHttpServerInstalled())
            {
                var dr =
                    MessageBox.
                    Show("Node JS http-server is not installed, would you like to install it?",
                         "Install http-server?",
                         MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    System.Threading.Tasks.Task.Run(() =>
                                                    this.InvokeIfRequired(() =>
                                                                          MessageBox.Show("Installing Http-server... Please wait for the popup", "Installing")
                                                                          ));
                    if (NodeJSUtil.InstallHttpServer())
                    {
                        MessageBox.Show("Successfully installed!", "Success");
                    }
                    else
                    {
                        MessageBox.Show("Failed To Install... Please run \"npm install http-server\" in cmd (without the quotes)\nExiting...", "Failed");
                        Environment.Exit(1);
                    }
                }
                else
                {
                }
            }
        }
Beispiel #2
0
            public void StartServer(PkgInfo info)
            {
                foreach (var proc in NodeJSUtil.GetNodeProcesses())
                {
                    NodeJsProcessSet.Add(proc.Id);
                }

                Logger.WriteLine("::StartServer - Starting server in directory " + Path.GetDirectoryName(info.FilePath), Logger.Type.StandardOutput);
                var cmdProcess = new Process();
                var path       = Path.GetDirectoryName(info.FilePath);

                cmdProcess.StartInfo.FileName               = "cmd.exe";
                cmdProcess.StartInfo.Arguments              = $"/C http-server \"{path}\" -p {GetNextBestPort()}";
                cmdProcess.StartInfo.UseShellExecute        = false;
                cmdProcess.StartInfo.CreateNoWindow         = true;
                cmdProcess.StartInfo.RedirectStandardOutput = true;
                cmdProcess.StartInfo.RedirectStandardError  = true;
                if (Directory.GetLogicalDrives().Contains(path))
                {
                    cmdProcess.StartInfo.Arguments = cmdProcess.StartInfo.Arguments.Replace(@"\", @"\\");
                }
                cmdProcess.Start();

                //Assumption will be that it should always be found
                currentProcessPid = 0;
                while (currentProcessPid == 0)
                {
                    try {
                        currentProcessPid = NodeJSUtil.GetNodeProcesses()
                                            .ToList()
                                            .Find(proc => !NodeJsProcessSet.Contains(proc.Id))
                                            .Id;
                    } catch (Exception) {
                    }
                    System.Threading.Thread.Sleep(100);
                }


                var temp = cmdProcess;

                System.Threading.Tasks.Task.Run(() => {
                    while (temp.Handle == IntPtr.Zero)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                    var stdout = "";
                    while (stdout != null)
                    {
                        stdout = temp.StandardOutput.ReadLine();
                        if (stdout != null)
                        {
                            Logger.WriteLine(stdout, Logger.Type.StandardOutput);
                        }
                    }
                });

                System.Threading.Tasks.Task.Run(() => {
                    while (temp.Handle == IntPtr.Zero)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                    var stderr = "";
                    while (stderr != null)
                    {
                        stderr = temp.StandardError.ReadLine();
                        if (stderr != null)
                        {
                            if (stderr.Contains("EADDRINUSE"))
                            {
                                server.IsRunning = false;
                                break;
                            }
                        }
                        Logger.WriteLine(stderr, Logger.Type.StandardOutput);
                    }
                });
                IsRunning = true;
            }
Beispiel #3
0
            public void StartServer(PkgInfo info)
            {
                foreach (var proc in NodeJSUtil.GetNodeProcesses())
                {
                    NodeJsProcessSet.Add(proc.Id);
                }
                Logger.WriteLine("::StartServer - Starting server in directory " + Path.GetDirectoryName(info.FilePath), Logger.Type.StandardOutput);
                var cmdProcess = new Process();
                var path       = Path.GetDirectoryName(info.FilePath);

                cmdProcess.StartInfo.FileName               = "cmd.exe";
                cmdProcess.StartInfo.Arguments              = $"/C http-server \"{path}\" -p {GetNextBestPort()}";
                cmdProcess.StartInfo.UseShellExecute        = false;
                cmdProcess.StartInfo.CreateNoWindow         = true;
                cmdProcess.StartInfo.RedirectStandardOutput = true;
                cmdProcess.StartInfo.RedirectStandardError  = true;
                if (Directory.GetLogicalDrives().Contains(path))
                {
                    cmdProcess.StartInfo.Arguments = cmdProcess.StartInfo.Arguments.Replace(@"\", @"\\");
                }
                cmdProcess.Start();

                //Assumption will be that it should always be found
                currentProcessPid = 0;
                while (currentProcessPid == 0)
                {
                    try {
                        var nodeProcesses = NodeJSUtil.GetNodeProcesses().ToList();
                        if (nodeProcesses.Count > 0)
                        {
                            currentProcessPid = nodeProcesses
                                                .Find(proc => !NodeJsProcessSet.Contains(proc.Id))
                                                .Id;
                        }
                    } catch (Exception e) {
                    }
                    System.Threading.Thread.Sleep(100);
                }

                var temp = cmdProcess;

                while (temp.Handle == IntPtr.Zero)
                {
                    System.Threading.Thread.Sleep(100);
                }

                Stopwatch stopwatchTimeoutTracker = new Stopwatch();

                stopwatchTimeoutTracker.Start();
                IsRunning = false;

                //Couldn't think of a better solution
                //Just gotta hope this works well
                //Otherwise we'll be stuck here forever :(
                var stdout = "";

                while (stdout != null)
                {
                    stdout = temp.StandardOutput.ReadLine();
                    if (stdout != null)
                    {
                        if (stdout.ToLower().Contains("starting up http-server, serving"))
                        {
                            IsRunning = true;
                            break;
                        }
                        Logger.WriteLine(stdout, Logger.Type.StandardOutput);
                    }
                }

                if (!IsRunning && temp.HasExited)
                {
                    var strdErr = temp.StandardError.ReadToEnd();
                    if (strdErr.Contains("EADDRINUSE"))
                    {
                        throw new ServerInitializationException("Address already in use");
                    }
                }
            }