PageRenderTime 449ms CodeModel.GetById 40ms RepoModel.GetById 16ms app.codeStats 0ms

/Kudu.Core/Deployment/NodeSiteEnabler.cs

https://github.com/moacap/kudu
C# | 117 lines | 95 code | 14 blank | 8 comment | 6 complexity | 796b38381b80bd9c8b29f2ebf3a120e6 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.IO.Abstractions;
  6. using System.Linq;
  7. using Kudu.Contracts.Tracing;
  8. using Kudu.Core.Infrastructure;
  9. namespace Kudu.Core.Deployment
  10. {
  11. public class NodeSiteEnabler
  12. {
  13. private IFileSystem _fileSystem;
  14. private string _siteFolder;
  15. private string _repoFolder;
  16. private string _scriptPath;
  17. private readonly string[] NodeStartFiles = new[] { "server.js", "app.js" };
  18. private readonly string[] NonNodeExtensions = new[] { "*.php", "*.htm", "*.html", "*.aspx", "*.cshtml" };
  19. private const string WebConfigFile = "web.config";
  20. private const string PackageJsonFile = "package.json";
  21. public NodeSiteEnabler(IFileSystem fileSystem, string repoFolder, string siteFolder, string scriptPath)
  22. {
  23. _fileSystem = fileSystem;
  24. _repoFolder = repoFolder;
  25. _siteFolder = siteFolder;
  26. _scriptPath = scriptPath;
  27. }
  28. public bool NeedNodeHandling()
  29. {
  30. // If there is a config file in the repo, we don't need to do anything
  31. if (_fileSystem.File.Exists(Path.Combine(_repoFolder, WebConfigFile)))
  32. {
  33. return false;
  34. }
  35. return this.LooksLikeNode();
  36. }
  37. public bool LooksLikeNode()
  38. {
  39. // If it has package.json at the root, it is node
  40. if (_fileSystem.File.Exists(Path.Combine(_siteFolder, PackageJsonFile)))
  41. {
  42. return true;
  43. }
  44. // If it has no .js files at the root, it's not Node
  45. if (!_fileSystem.Directory.GetFiles(_siteFolder, "*.js").Any())
  46. {
  47. return false;
  48. }
  49. // If it has a node_modules folder, it's likely Node
  50. if (_fileSystem.Directory.Exists(Path.Combine(_siteFolder, "node_modules")))
  51. {
  52. return true;
  53. }
  54. // If it has files that have a clear non-Node extension, treat it as non-Node
  55. foreach (var extension in NonNodeExtensions)
  56. {
  57. if (_fileSystem.Directory.GetFiles(_siteFolder, extension).Any())
  58. {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. public string GetNodeStartFile()
  65. {
  66. // Check if any of the known start pages exist
  67. foreach (var nodeDetectionFile in NodeStartFiles)
  68. {
  69. string fullPath = Path.Combine(_siteFolder, nodeDetectionFile);
  70. if (_fileSystem.File.Exists(fullPath))
  71. {
  72. return nodeDetectionFile;
  73. }
  74. }
  75. return null;
  76. }
  77. public void CreateConfigFile(string nodeStartFile)
  78. {
  79. _fileSystem.File.WriteAllText(
  80. Path.Combine(_siteFolder, WebConfigFile),
  81. String.Format(Resources.IisNodeWebConfig, nodeStartFile));
  82. }
  83. public string SelectNodeVersion(ITracer tracer)
  84. {
  85. // The node.js version selection logic is implemented in selectNodeVersion.js.
  86. // run with default node.js version which is on the path
  87. Executable executor = new Executable("node.exe", string.Empty);
  88. try
  89. {
  90. return executor.ExecuteWithConsoleOutput(
  91. tracer,
  92. "\"{0}\\selectNodeVersion.js\" \"{1}\" \"{2}\"",
  93. _scriptPath,
  94. _repoFolder,
  95. _siteFolder).Item1;
  96. }
  97. catch (Exception e)
  98. {
  99. throw new InvalidOperationException(Resources.Error_UnableToSelectNodeVersion, e);
  100. }
  101. }
  102. }
  103. }