nodejs.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package buildpacks
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "sync"
  8. "github.com/Masterminds/semver/v3"
  9. "github.com/google/go-github/github"
  10. )
  11. var (
  12. lts = map[string]int{
  13. "argon": 4,
  14. "boron": 6,
  15. "carbon": 8,
  16. "dubnium": 10,
  17. }
  18. )
  19. type nodejsRuntime struct {
  20. wg sync.WaitGroup
  21. }
  22. func NewNodeRuntime() Runtime {
  23. return &nodejsRuntime{}
  24. }
  25. func (runtime *nodejsRuntime) detectYarn(results chan struct {
  26. string
  27. bool
  28. }, directoryContent []*github.RepositoryContent) {
  29. yarnLockFound := false
  30. packageJSONFound := false
  31. for i := 0; i < len(directoryContent); i++ {
  32. name := directoryContent[i].GetName()
  33. if name == "yarn.lock" {
  34. yarnLockFound = true
  35. } else if name == "package.json" {
  36. packageJSONFound = true
  37. }
  38. if yarnLockFound && packageJSONFound {
  39. break
  40. }
  41. }
  42. if yarnLockFound && packageJSONFound {
  43. results <- struct {
  44. string
  45. bool
  46. }{yarn, true}
  47. }
  48. runtime.wg.Done()
  49. }
  50. func (runtime *nodejsRuntime) detectNPM(results chan struct {
  51. string
  52. bool
  53. }, directoryContent []*github.RepositoryContent) {
  54. packageJSONFound := false
  55. for i := 0; i < len(directoryContent); i++ {
  56. name := directoryContent[i].GetName()
  57. if name == "package.json" {
  58. packageJSONFound = true
  59. break
  60. }
  61. }
  62. if packageJSONFound {
  63. results <- struct {
  64. string
  65. bool
  66. }{npm, true}
  67. }
  68. runtime.wg.Done()
  69. }
  70. func (runtime *nodejsRuntime) detectStandalone(results chan struct {
  71. string
  72. bool
  73. }, directoryContent []*github.RepositoryContent) {
  74. jsFileFound := false
  75. for i := 0; i < len(directoryContent); i++ {
  76. name := directoryContent[i].GetName()
  77. if name == "server.js" || name == "app.js" || name == "main.js" || name == "index.js" {
  78. jsFileFound = true
  79. break
  80. }
  81. }
  82. if jsFileFound {
  83. results <- struct {
  84. string
  85. bool
  86. }{standalone, true}
  87. }
  88. runtime.wg.Done()
  89. }
  90. // copied directly from https://github.com/paketo-buildpacks/node-engine/blob/main/nvmrc_parser.go
  91. func validateNvmrc(content string) (string, error) {
  92. content = strings.TrimSpace(strings.ToLower(content))
  93. if content == "lts/*" || content == "node" {
  94. return content, nil
  95. }
  96. for key := range lts {
  97. if content == strings.ToLower("lts/"+key) {
  98. return content, nil
  99. }
  100. }
  101. content = strings.TrimPrefix(content, "v")
  102. if _, err := semver.NewConstraint(content); err != nil {
  103. return "", fmt.Errorf("invalid version constraint specified in .nvmrc: %q", content)
  104. }
  105. return content, nil
  106. }
  107. // copied directly from https://github.com/paketo-buildpacks/node-engine/blob/main/nvmrc_parser.go
  108. func formatNvmrcContent(version string) string {
  109. if version == "node" {
  110. return "*"
  111. }
  112. if strings.HasPrefix(version, "lts") {
  113. ltsName := strings.SplitN(version, "/", 2)[1]
  114. if ltsName == "*" {
  115. var maxVersion int
  116. for _, versionValue := range lts {
  117. if maxVersion < versionValue {
  118. maxVersion = versionValue
  119. }
  120. }
  121. return fmt.Sprintf("%d.*", maxVersion)
  122. }
  123. return fmt.Sprintf("%d.*", lts[ltsName])
  124. }
  125. return version
  126. }
  127. // copied directly from https://github.com/paketo-buildpacks/node-engine/blob/main/node_version_parser.go
  128. func validateNodeVersion(content string) (string, error) {
  129. content = strings.TrimSpace(strings.ToLower(content))
  130. content = strings.TrimPrefix(content, "v")
  131. if _, err := semver.NewConstraint(content); err != nil {
  132. return "", fmt.Errorf("invalid version constraint specified in .node-version: %q", content)
  133. }
  134. return content, nil
  135. }
  136. func (runtime *nodejsRuntime) Detect(
  137. client *github.Client,
  138. directoryContent []*github.RepositoryContent,
  139. owner, name, path string,
  140. repoContentOptions github.RepositoryContentGetOptions,
  141. paketo, heroku *BuilderInfo,
  142. ) error {
  143. results := make(chan struct {
  144. string
  145. bool
  146. }, 3)
  147. runtime.wg.Add(3)
  148. go runtime.detectYarn(results, directoryContent)
  149. go runtime.detectNPM(results, directoryContent)
  150. go runtime.detectStandalone(results, directoryContent)
  151. runtime.wg.Wait()
  152. close(results)
  153. paketoBuildpackInfo := BuildpackInfo{
  154. Name: "NodeJS",
  155. Buildpack: "paketobuildpacks/nodejs",
  156. }
  157. herokuBuildpackInfo := BuildpackInfo{
  158. Name: "NodeJS",
  159. Buildpack: "heroku/nodejs",
  160. }
  161. if len(results) == 0 {
  162. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  163. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  164. return nil
  165. }
  166. foundYarn := false
  167. foundNPM := false
  168. foundStandalone := false
  169. for result := range results {
  170. if result.string == yarn {
  171. foundYarn = true
  172. } else if result.string == npm {
  173. foundNPM = true
  174. } else if result.string == standalone {
  175. foundStandalone = true
  176. }
  177. }
  178. if foundYarn || foundNPM {
  179. // it is safe to assume that the project contains a package.json
  180. fileContent, _, _, err := client.Repositories.GetContents(
  181. context.Background(),
  182. owner,
  183. name,
  184. fmt.Sprintf("%s/package.json", path),
  185. &repoContentOptions,
  186. )
  187. if err != nil {
  188. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  189. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  190. return fmt.Errorf("error fetching contents of package.json: %v", err)
  191. }
  192. var packageJSON struct {
  193. Scripts map[string]string `json:"scripts"`
  194. Engines struct {
  195. Node string `json:"node"`
  196. } `json:"engines"`
  197. }
  198. data, err := fileContent.GetContent()
  199. if err != nil {
  200. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  201. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  202. return fmt.Errorf("error calling GetContent() on package.json: %v", err)
  203. }
  204. err = json.NewDecoder(strings.NewReader(data)).Decode(&packageJSON)
  205. if err != nil {
  206. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  207. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  208. return fmt.Errorf("error decoding package.json contents to struct: %v", err)
  209. }
  210. if packageJSON.Engines.Node == "" {
  211. // we should now check for the node engine version in .nvmrc and then .node-version
  212. nvmrcFound := false
  213. nodeVersionFound := false
  214. for i := 0; i < len(directoryContent); i++ {
  215. name := directoryContent[i].GetName()
  216. if name == ".nvmrc" {
  217. nvmrcFound = true
  218. } else if name == ".node-version" {
  219. nodeVersionFound = true
  220. }
  221. }
  222. if nvmrcFound {
  223. // copy exact behavior of https://github.com/paketo-buildpacks/node-engine/blob/main/nvmrc_parser.go
  224. fileContent, _, _, err = client.Repositories.GetContents(
  225. context.Background(),
  226. owner,
  227. name,
  228. fmt.Sprintf("%s/.nvmrc", path),
  229. &repoContentOptions,
  230. )
  231. if err != nil {
  232. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  233. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  234. return fmt.Errorf("error fetching contents of .nvmrc: %v", err)
  235. }
  236. data, err = fileContent.GetContent()
  237. if err != nil {
  238. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  239. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  240. return fmt.Errorf("error calling GetContent() on .nvmrc: %v", err)
  241. }
  242. nvmrcVersion, err := validateNvmrc(data)
  243. if err != nil {
  244. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  245. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  246. return fmt.Errorf("error validating .nvmrc: %v", err)
  247. }
  248. nvmrcVersion = formatNvmrcContent(nvmrcVersion)
  249. if nvmrcVersion != "*" {
  250. packageJSON.Engines.Node = data
  251. }
  252. }
  253. if packageJSON.Engines.Node == "" && nodeVersionFound {
  254. // copy exact behavior of https://github.com/paketo-buildpacks/node-engine/blob/main/node_version_parser.go
  255. fileContent, _, _, err = client.Repositories.GetContents(
  256. context.Background(),
  257. owner,
  258. name,
  259. fmt.Sprintf("%s/.node-version", path),
  260. &repoContentOptions,
  261. )
  262. if err != nil {
  263. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  264. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  265. return fmt.Errorf("error fetching contents of .node-version: %v", err)
  266. }
  267. data, err = fileContent.GetContent()
  268. if err != nil {
  269. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  270. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  271. return fmt.Errorf("error calling GetContent() on .node-version: %v", err)
  272. }
  273. nodeVersion, err := validateNodeVersion(data)
  274. if err != nil {
  275. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  276. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  277. return fmt.Errorf("error validating .node-version: %v", err)
  278. }
  279. if nodeVersion != "" {
  280. packageJSON.Engines.Node = nodeVersion
  281. }
  282. }
  283. }
  284. if packageJSON.Engines.Node == "" {
  285. // use the default node engine version from https://github.com/paketo-buildpacks/node-engine/blob/main/buildpack.toml
  286. packageJSON.Engines.Node = "16.*.*"
  287. }
  288. paketoBuildpackInfo.Config = make(map[string]interface{})
  289. paketoBuildpackInfo.Config["scripts"] = packageJSON.Scripts
  290. paketoBuildpackInfo.Config["node_engine"] = packageJSON.Engines.Node
  291. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  292. herokuBuildpackInfo.Config = make(map[string]interface{})
  293. herokuBuildpackInfo.Config["scripts"] = packageJSON.Scripts
  294. herokuBuildpackInfo.Config["node_engine"] = packageJSON.Engines.Node
  295. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  296. } else if foundStandalone {
  297. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  298. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  299. }
  300. return nil
  301. }