Example #1
0
        private string GetStartupCommandFromJsFile(RunScriptGeneratorOptions options, string file)
        {
            var command = string.Empty;

            if (!string.IsNullOrWhiteSpace(options.CustomServerCommand))
            {
                _logger.LogInformation("Using custom server command {nodeCommand}", options.CustomServerCommand);
                command = $"{options.CustomServerCommand.Trim()} {file}";
            }
            else
            {
                switch (options.DebuggingMode)
                {
                case DebuggingMode.Standard:
                    _logger.LogInformation("Debugging in standard mode");
                    command = $"node --inspect {file}";
                    break;

                case DebuggingMode.Break:
                    _logger.LogInformation("Debugging in break mode");
                    command = $"node --inspect-brk {file}";
                    break;

                case DebuggingMode.None:
                    _logger.LogInformation("Running without debugging");
                    command = $"node {file}";
                    break;
                }
            }

            return(command);
        }
Example #2
0
        internal override int Execute(IServiceProvider serviceProvider, IConsole console)
        {
            if (string.IsNullOrWhiteSpace(PlatformName))
            {
                console.WriteLine("Platform name is required.");
                return(ProcessConstants.ExitFailure);
            }

            AppPath = string.IsNullOrWhiteSpace(AppPath) ? "." : AppPath;
            string         appFullPath        = Path.GetFullPath(AppPath);
            string         defaultAppFullPath = string.IsNullOrWhiteSpace(DefaultApp) ? null : Path.GetFullPath(DefaultApp);
            ILoggerFactory loggerFactory      = serviceProvider.GetRequiredService <ILoggerFactory>();
            var            sourceRepo         = new LocalSourceRepo(appFullPath, loggerFactory);

            var options = new RunScriptGeneratorOptions
            {
                CustomServerCommand = ServerCmd,
                DebuggingMode       = DebugMode,
                DebugPort           = DebugPort,
                DefaultAppPath      = defaultAppFullPath,
                SourceRepo          = sourceRepo,
                UserStartupCommand  = UserStartupCommand,
                PlatformVersion     = PlatformVersion,
                BindPort            = BindPort
            };

            var runScriptGenerator = serviceProvider.GetRequiredService <IRunScriptGenerator>();
            var script             = runScriptGenerator.GenerateBashScript(PlatformName, options);

            if (string.IsNullOrEmpty(script))
            {
                console.Error.WriteLine("Error: Couldn't generate startup script.");
                return(ProcessConstants.ExitFailure);
            }

            if (string.IsNullOrWhiteSpace(OutputPath))
            {
                console.WriteLine(script);
            }
            else
            {
                File.WriteAllText(OutputPath, script);
                console.WriteLine($"Script written to '{OutputPath}'");

                // Try making the script executable
                ProcessHelper.TrySetExecutableMode(OutputPath);
            }

            return(ProcessConstants.ExitSuccess);
        }
        public void RunUserScriptIfProvided()
        {
            // Arrange
            var options = new RunScriptGeneratorOptions();

            options.SourceRepo         = new MemorySourceRepo();
            options.UserStartupCommand = "abc.sh";
            var platform = GetPlatform();

            // Act
            var script = platform.GenerateBashRunScript(options);

            // Assert
            Assert.NotNull(script);
            Assert.Contains("abc.sh", script);
        }
        public void DefaultAppIfNoCommandDetected()
        {
            // Arrange
            var repo    = new MemorySourceRepo();
            var options = new RunScriptGeneratorOptions();

            options.SourceRepo     = repo;
            options.DefaultAppPath = "default.js";
            var platform = GetPlatform();

            // Act
            var script = platform.GenerateBashRunScript(options);

            // Assert
            Assert.NotNull(script);
            Assert.Contains("node default.js", script);
        }
        public void NpmStartIfInPackageJson()
        {
            // Arrange
            var repo = new MemorySourceRepo();

            repo.AddFile(PackageJsonWithStartScript, NodeConstants.PackageJsonFileName);
            var options = new RunScriptGeneratorOptions();

            options.SourceRepo = repo;
            var platform = GetPlatform();

            // Act
            var script = platform.GenerateBashRunScript(options);

            // Assert
            Assert.NotNull(script);
            Assert.Contains("npm start", script);
        }
        public void NodeStartFromListOfCandidateFiles(string filename)
        {
            // Arrange
            var repo = new MemorySourceRepo();

            repo.AddFile(PackageJsonWithoutScript, NodeConstants.PackageJsonFileName);
            repo.AddFile("content", filename);
            var options = new RunScriptGeneratorOptions();

            options.SourceRepo = repo;
            var platform = GetPlatform();

            // Act
            var script = platform.GenerateBashRunScript(options);

            // Assert
            Assert.NotNull(script);
            Assert.Contains($"node {filename}", script);
        }
        public void NodeAddsBenv_IfPlatformVersionSupplied()
        {
            // Arrange
            var repo = new MemorySourceRepo();

            repo.AddFile(PackageJsonWithMainScript, NodeConstants.PackageJsonFileName);
            var options = new RunScriptGeneratorOptions();

            options.SourceRepo    = repo;
            options.DebuggingMode = DebuggingMode.Standard;
            var platform = GetPlatform();

            // Act
            var script = platform.GenerateBashRunScript(options);

            // Assert
            Assert.NotNull(script);
            Assert.DoesNotContain("source /usr/local/bin/benv", script);
        }
        public void NodeStartIfMainJsFile_debug()
        {
            // Arrange
            var repo = new MemorySourceRepo();

            repo.AddFile(PackageJsonWithMainScript, NodeConstants.PackageJsonFileName);
            var options = new RunScriptGeneratorOptions();

            options.SourceRepo    = repo;
            options.DebuggingMode = DebuggingMode.Standard;
            var platform = GetPlatform();

            // Act
            var script = platform.GenerateBashRunScript(options);

            // Assert
            Assert.NotNull(script);
            Assert.Contains("node --inspect server.js", script);
        }
        public void NodeStartIfMainJsFile_customServer()
        {
            // Arrange
            var repo = new MemorySourceRepo();

            repo.AddFile(PackageJsonWithMainScript, NodeConstants.PackageJsonFileName);
            var options = new RunScriptGeneratorOptions();

            options.SourceRepo          = repo;
            options.CustomServerCommand = "pm2 --test";
            var platform = GetPlatform();

            // Act
            var script = platform.GenerateBashRunScript(options);

            // Assert
            Assert.NotNull(script);
            Assert.Contains("pm2 --test server.js", script);
        }
Example #10
0
        internal override int Execute(IServiceProvider serviceProvider, IConsole console)
        {
            if (string.IsNullOrWhiteSpace(PlatformName))
            {
                console.Error.WriteLine("Platform name is required.");
                return(ProcessConstants.ExitFailure);
            }

            string         appPath       = Path.GetFullPath(AppDir);
            ILoggerFactory loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();
            var            sourceRepo    = new LocalSourceRepo(appPath, loggerFactory);

            var options = new RunScriptGeneratorOptions
            {
                SourceRepo        = sourceRepo,
                PlatformVersion   = PlatformVersion,
                PassThruArguments = RemainingArgs
            };

            var runScriptGenerator = serviceProvider.GetRequiredService <IRunScriptGenerator>();
            var script             = runScriptGenerator.GenerateBashScript(PlatformName, options);

            if (string.IsNullOrEmpty(script))
            {
                console.Error.WriteLine("Error: Couldn't generate startup script.");
                return(ProcessConstants.ExitFailure);
            }

            if (string.IsNullOrWhiteSpace(OutputPath))
            {
                console.WriteLine(script);
            }
            else
            {
                File.WriteAllText(OutputPath, script);
                console.WriteLine($"Script written to '{OutputPath}'");

                // Try making the script executable
                ProcessHelper.TrySetExecutableMode(OutputPath);
            }

            return(ProcessConstants.ExitSuccess);
        }
 public string GenerateBashRunScript(RunScriptGeneratorOptions runScriptGeneratorOptions)
 {
     throw new NotImplementedException();
 }
Example #12
0
        public string GenerateBashRunScript(RunScriptGeneratorOptions options)
        {
            if (options.SourceRepo == null)
            {
                throw new ArgumentNullException(nameof(RunScriptGeneratorOptions.SourceRepo));
            }

            string startupCommand = null;

            // Log how we detected the entrypoint command
            var commandSource = string.Empty;

            if (!string.IsNullOrWhiteSpace(options.UserStartupCommand))
            {
                startupCommand = options.UserStartupCommand.Trim();
                _logger.LogInformation("Using user-provided startup command");
                commandSource = "User";
            }
            else
            {
                var packageJson = GetPackageJsonObject(options.SourceRepo, _logger);
                startupCommand = packageJson?.scripts?.start;
                if (string.IsNullOrWhiteSpace(startupCommand))
                {
                    string mainJsFile = packageJson?.main;
                    if (string.IsNullOrEmpty(mainJsFile))
                    {
                        var candidateFiles = new[] { "bin/www", "server.js", "app.js", "index.js", "hostingstart.js" };
                        foreach (var file in candidateFiles)
                        {
                            if (options.SourceRepo.FileExists(file))
                            {
                                startupCommand = GetStartupCommandFromJsFile(options, file);
                                _logger.LogInformation("Found startup candidate {nodeStartupFile}", file);
                                commandSource = "CandidateFile";
                                break;
                            }
                        }
                    }
                    else
                    {
                        startupCommand = GetStartupCommandFromJsFile(options, mainJsFile);
                        commandSource  = "PackageJsonMain";
                    }
                }
                else
                {
                    if (options.SourceRepo.FileExists(NodeConstants.YarnLockFileName))
                    {
                        commandSource  = "PackageJsonStartYarn";
                        startupCommand = NodeConstants.YarnStartCommand;
                        _logger.LogInformation("Found startup command in package.json, and will use Yarn");
                    }
                    else
                    {
                        commandSource  = "PackageJsonStartNpm";
                        startupCommand = NodeConstants.NpmStartCommand;
                        _logger.LogInformation("Found startup command in package.json, and will use npm");
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(startupCommand))
            {
                startupCommand = GetStartupCommandFromJsFile(options, options.DefaultAppPath);
                commandSource  = "DefaultApp";
            }

            _logger.LogInformation("Finalizing entrypoint script using {commandSource}", commandSource);
            var templateValues = new NodeBashRunScriptProperties
            {
                AppDirectory   = options.SourceRepo.RootPath,
                StartupCommand = startupCommand,
                ToolsVersions  = string.IsNullOrWhiteSpace(options.PlatformVersion) ? null : $"node={options.PlatformVersion}",
                BindPort       = options.BindPort
            };
            var script = TemplateHelpers.Render(TemplateHelpers.TemplateResource.NodeRunScript, templateValues);

            return(script);
        }
Example #13
0
 public string GenerateBashRunScript(RunScriptGeneratorOptions opts)
 {
     return(string.Empty);
 }