api_nodejs.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package buildpacks
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strings"
  6. "sync"
  7. "github.com/google/go-github/github"
  8. )
  9. type apiNodeRuntime struct {
  10. ghClient *github.Client
  11. wg sync.WaitGroup
  12. }
  13. func NewAPINodeRuntime(client *github.Client) *apiNodeRuntime {
  14. return &apiNodeRuntime{
  15. ghClient: client,
  16. }
  17. }
  18. func (runtime *apiNodeRuntime) detectYarn(results chan struct {
  19. string
  20. bool
  21. }, directoryContent []*github.RepositoryContent) {
  22. yarnLockFound := false
  23. packageJSONFound := false
  24. for i := 0; i < len(directoryContent); i++ {
  25. name := directoryContent[i].GetName()
  26. if name == "yarn.lock" {
  27. yarnLockFound = true
  28. } else if name == "package.json" {
  29. packageJSONFound = true
  30. }
  31. if yarnLockFound && packageJSONFound {
  32. break
  33. }
  34. }
  35. if yarnLockFound && packageJSONFound {
  36. results <- struct {
  37. string
  38. bool
  39. }{yarn, true}
  40. } else {
  41. results <- struct {
  42. string
  43. bool
  44. }{yarn, false}
  45. }
  46. runtime.wg.Done()
  47. }
  48. func (runtime *apiNodeRuntime) detectNPM(results chan struct {
  49. string
  50. bool
  51. }, directoryContent []*github.RepositoryContent) {
  52. packageJSONFound := false
  53. for i := 0; i < len(directoryContent); i++ {
  54. name := directoryContent[i].GetName()
  55. if name == "package.json" {
  56. packageJSONFound = true
  57. break
  58. }
  59. }
  60. if packageJSONFound {
  61. results <- struct {
  62. string
  63. bool
  64. }{npm, true}
  65. } else {
  66. results <- struct {
  67. string
  68. bool
  69. }{npm, false}
  70. }
  71. runtime.wg.Done()
  72. }
  73. func (runtime *apiNodeRuntime) detectStandalone(results chan struct {
  74. string
  75. bool
  76. }, directoryContent []*github.RepositoryContent) {
  77. jsFileFound := false
  78. for i := 0; i < len(directoryContent); i++ {
  79. name := directoryContent[i].GetName()
  80. if name == "server.js" || name == "app.js" || name == "main.js" || name == "index.js" {
  81. jsFileFound = true
  82. break
  83. }
  84. }
  85. if jsFileFound {
  86. results <- struct {
  87. string
  88. bool
  89. }{standalone, true}
  90. } else {
  91. results <- struct {
  92. string
  93. bool
  94. }{standalone, false}
  95. }
  96. runtime.wg.Done()
  97. }
  98. func (runtime *apiNodeRuntime) Detect(
  99. directoryContent []*github.RepositoryContent,
  100. owner string, name string,
  101. repoContentOptions github.RepositoryContentGetOptions,
  102. ) map[string]interface{} {
  103. results := make(chan struct {
  104. string
  105. bool
  106. }, 3)
  107. runtime.wg.Add(3)
  108. go runtime.detectYarn(results, directoryContent)
  109. go runtime.detectNPM(results, directoryContent)
  110. go runtime.detectStandalone(results, directoryContent)
  111. runtime.wg.Wait()
  112. close(results)
  113. atLeastOne := false
  114. detected := make(map[string]bool)
  115. for result := range results {
  116. if result.bool {
  117. atLeastOne = true
  118. }
  119. detected[result.string] = result.bool
  120. }
  121. if atLeastOne {
  122. if detected[yarn] || detected[npm] {
  123. // it is safe to assume that the project contains a package.json
  124. fileContent, _, _, err := runtime.ghClient.Repositories.GetContents(
  125. context.Background(),
  126. owner,
  127. name,
  128. "package.json",
  129. &repoContentOptions,
  130. )
  131. if err != nil {
  132. // FIXME: log somewhere
  133. return nil
  134. }
  135. var packageJSON struct {
  136. Scripts map[string]string `json:"scripts"`
  137. Engines struct {
  138. Node string `json:"node"`
  139. } `json:"engines"`
  140. }
  141. err = json.NewDecoder(strings.NewReader(*fileContent.Content)).Decode(&packageJSON)
  142. if err != nil {
  143. // FIXME: log somewhere
  144. return nil
  145. }
  146. if detected[yarn] {
  147. return map[string]interface{}{"runtime": yarn, "scripts": packageJSON.Scripts, "node_engine": packageJSON.Engines.Node}
  148. } else {
  149. return map[string]interface{}{"runtime": npm, "scripts": packageJSON.Scripts, "node_engine": packageJSON.Engines.Node}
  150. }
  151. }
  152. return map[string]interface{}{"runtime": "node-standalone"}
  153. }
  154. return nil
  155. }