nodejs.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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/v41/github"
  10. "github.com/xanzy/go-gitlab"
  11. )
  12. var (
  13. lts = map[string]int{
  14. "argon": 4,
  15. "boron": 6,
  16. "carbon": 8,
  17. "dubnium": 10,
  18. }
  19. )
  20. type nodejsRuntime struct {
  21. wg sync.WaitGroup
  22. }
  23. func NewNodeRuntime() Runtime {
  24. return &nodejsRuntime{}
  25. }
  26. func (runtime *nodejsRuntime) detectYarnGithub(results chan struct {
  27. string
  28. bool
  29. }, directoryContent []*github.RepositoryContent) {
  30. yarnLockFound := false
  31. packageJSONFound := false
  32. for i := 0; i < len(directoryContent); i++ {
  33. name := directoryContent[i].GetName()
  34. if name == "yarn.lock" {
  35. yarnLockFound = true
  36. } else if name == "package.json" {
  37. packageJSONFound = true
  38. }
  39. if yarnLockFound && packageJSONFound {
  40. break
  41. }
  42. }
  43. if yarnLockFound && packageJSONFound {
  44. results <- struct {
  45. string
  46. bool
  47. }{yarn, true}
  48. }
  49. runtime.wg.Done()
  50. }
  51. func (runtime *nodejsRuntime) detectYarnGitlab(results chan struct {
  52. string
  53. bool
  54. }, tree []*gitlab.TreeNode) {
  55. yarnLockFound := false
  56. packageJSONFound := false
  57. for i := 0; i < len(tree); i++ {
  58. name := tree[i].Name
  59. if name == "yarn.lock" {
  60. yarnLockFound = true
  61. } else if name == "package.json" {
  62. packageJSONFound = true
  63. }
  64. if yarnLockFound && packageJSONFound {
  65. break
  66. }
  67. }
  68. if yarnLockFound && packageJSONFound {
  69. results <- struct {
  70. string
  71. bool
  72. }{yarn, true}
  73. }
  74. runtime.wg.Done()
  75. }
  76. func (runtime *nodejsRuntime) detectNPMGithub(results chan struct {
  77. string
  78. bool
  79. }, directoryContent []*github.RepositoryContent) {
  80. packageJSONFound := false
  81. for i := 0; i < len(directoryContent); i++ {
  82. name := directoryContent[i].GetName()
  83. if name == "package.json" {
  84. packageJSONFound = true
  85. break
  86. }
  87. }
  88. if packageJSONFound {
  89. results <- struct {
  90. string
  91. bool
  92. }{npm, true}
  93. }
  94. runtime.wg.Done()
  95. }
  96. func (runtime *nodejsRuntime) detectNPMGitlab(results chan struct {
  97. string
  98. bool
  99. }, tree []*gitlab.TreeNode) {
  100. packageJSONFound := false
  101. for i := 0; i < len(tree); i++ {
  102. name := tree[i].Name
  103. if name == "package.json" {
  104. packageJSONFound = true
  105. break
  106. }
  107. }
  108. if packageJSONFound {
  109. results <- struct {
  110. string
  111. bool
  112. }{npm, true}
  113. }
  114. runtime.wg.Done()
  115. }
  116. func (runtime *nodejsRuntime) detectStandaloneGithub(results chan struct {
  117. string
  118. bool
  119. }, directoryContent []*github.RepositoryContent) {
  120. jsFileFound := false
  121. for i := 0; i < len(directoryContent); i++ {
  122. name := directoryContent[i].GetName()
  123. if name == "server.js" || name == "app.js" || name == "main.js" || name == "index.js" {
  124. jsFileFound = true
  125. break
  126. }
  127. }
  128. if jsFileFound {
  129. results <- struct {
  130. string
  131. bool
  132. }{standalone, true}
  133. }
  134. runtime.wg.Done()
  135. }
  136. func (runtime *nodejsRuntime) detectStandaloneGitlab(results chan struct {
  137. string
  138. bool
  139. }, tree []*gitlab.TreeNode) {
  140. jsFileFound := false
  141. for i := 0; i < len(tree); i++ {
  142. name := tree[i].Name
  143. if name == "server.js" || name == "app.js" || name == "main.js" || name == "index.js" {
  144. jsFileFound = true
  145. break
  146. }
  147. }
  148. if jsFileFound {
  149. results <- struct {
  150. string
  151. bool
  152. }{standalone, true}
  153. }
  154. runtime.wg.Done()
  155. }
  156. // copied directly from https://github.com/paketo-buildpacks/node-engine/blob/main/nvmrc_parser.go
  157. func validateNvmrc(content string) (string, error) {
  158. content = strings.TrimSpace(strings.ToLower(content))
  159. if content == "lts/*" || content == "node" {
  160. return content, nil
  161. }
  162. for key := range lts {
  163. if content == strings.ToLower("lts/"+key) {
  164. return content, nil
  165. }
  166. }
  167. content = strings.TrimPrefix(content, "v")
  168. if _, err := semver.NewConstraint(content); err != nil {
  169. return "", fmt.Errorf("invalid version constraint specified in .nvmrc: %q", content)
  170. }
  171. return content, nil
  172. }
  173. // copied directly from https://github.com/paketo-buildpacks/node-engine/blob/main/nvmrc_parser.go
  174. func formatNvmrcContent(version string) string {
  175. if version == "node" {
  176. return "*"
  177. }
  178. if strings.HasPrefix(version, "lts") {
  179. ltsName := strings.SplitN(version, "/", 2)[1]
  180. if ltsName == "*" {
  181. var maxVersion int
  182. for _, versionValue := range lts {
  183. if maxVersion < versionValue {
  184. maxVersion = versionValue
  185. }
  186. }
  187. return fmt.Sprintf("%d.*", maxVersion)
  188. }
  189. return fmt.Sprintf("%d.*", lts[ltsName])
  190. }
  191. return version
  192. }
  193. // copied directly from https://github.com/paketo-buildpacks/node-engine/blob/main/node_version_parser.go
  194. func validateNodeVersion(content string) (string, error) {
  195. content = strings.TrimSpace(strings.ToLower(content))
  196. content = strings.TrimPrefix(content, "v")
  197. if _, err := semver.NewConstraint(content); err != nil {
  198. return "", fmt.Errorf("invalid version constraint specified in .node-version: %q", content)
  199. }
  200. return content, nil
  201. }
  202. func (runtime *nodejsRuntime) DetectGithub(
  203. client *github.Client,
  204. directoryContent []*github.RepositoryContent,
  205. owner, name, path string,
  206. repoContentOptions github.RepositoryContentGetOptions,
  207. paketo, heroku *BuilderInfo,
  208. ) error {
  209. results := make(chan struct {
  210. string
  211. bool
  212. }, 3)
  213. runtime.wg.Add(3)
  214. go runtime.detectYarnGithub(results, directoryContent)
  215. go runtime.detectNPMGithub(results, directoryContent)
  216. go runtime.detectStandaloneGithub(results, directoryContent)
  217. runtime.wg.Wait()
  218. close(results)
  219. paketoBuildpackInfo := BuildpackInfo{
  220. Name: "NodeJS",
  221. Buildpack: "gcr.io/paketo-buildpacks/nodejs",
  222. }
  223. herokuBuildpackInfo := BuildpackInfo{
  224. Name: "NodeJS",
  225. Buildpack: "heroku/nodejs",
  226. }
  227. if len(results) == 0 {
  228. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  229. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  230. return nil
  231. }
  232. foundYarn := false
  233. foundNPM := false
  234. foundStandalone := false
  235. for result := range results {
  236. if result.string == yarn {
  237. foundYarn = true
  238. } else if result.string == npm {
  239. foundNPM = true
  240. } else if result.string == standalone {
  241. foundStandalone = true
  242. }
  243. }
  244. if foundYarn || foundNPM {
  245. // it is safe to assume that the project contains a package.json
  246. fileContent, _, _, err := client.Repositories.GetContents(
  247. context.Background(),
  248. owner,
  249. name,
  250. fmt.Sprintf("%s/package.json", path),
  251. &repoContentOptions,
  252. )
  253. if err != nil {
  254. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  255. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  256. return fmt.Errorf("error fetching contents of package.json: %v", err)
  257. }
  258. var packageJSON struct {
  259. Scripts map[string]string `json:"scripts"`
  260. Engines struct {
  261. Node string `json:"node"`
  262. } `json:"engines"`
  263. }
  264. data, err := fileContent.GetContent()
  265. if err != nil {
  266. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  267. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  268. return fmt.Errorf("error calling GetContent() on package.json: %v", err)
  269. }
  270. err = json.NewDecoder(strings.NewReader(data)).Decode(&packageJSON)
  271. if err != nil {
  272. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  273. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  274. return fmt.Errorf("error decoding package.json contents to struct: %v", err)
  275. }
  276. if packageJSON.Engines.Node == "" {
  277. // we should now check for the node engine version in .nvmrc and then .node-version
  278. nvmrcFound := false
  279. nodeVersionFound := false
  280. for i := 0; i < len(directoryContent); i++ {
  281. name := directoryContent[i].GetName()
  282. if name == ".nvmrc" {
  283. nvmrcFound = true
  284. } else if name == ".node-version" {
  285. nodeVersionFound = true
  286. }
  287. }
  288. if nvmrcFound {
  289. // copy exact behavior of https://github.com/paketo-buildpacks/node-engine/blob/main/nvmrc_parser.go
  290. fileContent, _, _, err = client.Repositories.GetContents(
  291. context.Background(),
  292. owner,
  293. name,
  294. fmt.Sprintf("%s/.nvmrc", path),
  295. &repoContentOptions,
  296. )
  297. if err != nil {
  298. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  299. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  300. return fmt.Errorf("error fetching contents of .nvmrc: %v", err)
  301. }
  302. data, err = fileContent.GetContent()
  303. if err != nil {
  304. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  305. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  306. return fmt.Errorf("error calling GetContent() on .nvmrc: %v", err)
  307. }
  308. nvmrcVersion, err := validateNvmrc(data)
  309. if err != nil {
  310. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  311. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  312. return fmt.Errorf("error validating .nvmrc: %v", err)
  313. }
  314. nvmrcVersion = formatNvmrcContent(nvmrcVersion)
  315. if nvmrcVersion != "*" {
  316. packageJSON.Engines.Node = data
  317. }
  318. }
  319. if packageJSON.Engines.Node == "" && nodeVersionFound {
  320. // copy exact behavior of https://github.com/paketo-buildpacks/node-engine/blob/main/node_version_parser.go
  321. fileContent, _, _, err = client.Repositories.GetContents(
  322. context.Background(),
  323. owner,
  324. name,
  325. fmt.Sprintf("%s/.node-version", path),
  326. &repoContentOptions,
  327. )
  328. if err != nil {
  329. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  330. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  331. return fmt.Errorf("error fetching contents of .node-version: %v", err)
  332. }
  333. data, err = fileContent.GetContent()
  334. if err != nil {
  335. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  336. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  337. return fmt.Errorf("error calling GetContent() on .node-version: %v", err)
  338. }
  339. nodeVersion, err := validateNodeVersion(data)
  340. if err != nil {
  341. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  342. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  343. return fmt.Errorf("error validating .node-version: %v", err)
  344. }
  345. if nodeVersion != "" {
  346. packageJSON.Engines.Node = nodeVersion
  347. }
  348. }
  349. }
  350. if packageJSON.Engines.Node == "" {
  351. // use the default node engine version from https://github.com/paketo-buildpacks/node-engine/blob/main/buildpack.toml
  352. packageJSON.Engines.Node = "16.*.*"
  353. }
  354. paketoBuildpackInfo.Config = make(map[string]interface{})
  355. paketoBuildpackInfo.Config["scripts"] = packageJSON.Scripts
  356. paketoBuildpackInfo.Config["node_engine"] = packageJSON.Engines.Node
  357. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  358. herokuBuildpackInfo.Config = make(map[string]interface{})
  359. herokuBuildpackInfo.Config["scripts"] = packageJSON.Scripts
  360. herokuBuildpackInfo.Config["node_engine"] = packageJSON.Engines.Node
  361. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  362. } else if foundStandalone {
  363. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  364. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  365. }
  366. return nil
  367. }
  368. func (runtime *nodejsRuntime) DetectGitlab(
  369. client *gitlab.Client,
  370. tree []*gitlab.TreeNode,
  371. owner, name, path, ref string,
  372. paketo, heroku *BuilderInfo,
  373. ) error {
  374. results := make(chan struct {
  375. string
  376. bool
  377. }, 3)
  378. runtime.wg.Add(3)
  379. go runtime.detectYarnGitlab(results, tree)
  380. go runtime.detectNPMGitlab(results, tree)
  381. go runtime.detectStandaloneGitlab(results, tree)
  382. runtime.wg.Wait()
  383. close(results)
  384. paketoBuildpackInfo := BuildpackInfo{
  385. Name: "NodeJS",
  386. Buildpack: "gcr.io/paketo-buildpacks/nodejs",
  387. }
  388. herokuBuildpackInfo := BuildpackInfo{
  389. Name: "NodeJS",
  390. Buildpack: "heroku/nodejs",
  391. }
  392. if len(results) == 0 {
  393. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  394. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  395. return nil
  396. }
  397. // foundYarn := false
  398. // foundNPM := false
  399. // foundStandalone := false
  400. // for result := range results {
  401. // if result.string == yarn {
  402. // foundYarn = true
  403. // } else if result.string == npm {
  404. // foundNPM = true
  405. // } else if result.string == standalone {
  406. // foundStandalone = true
  407. // }
  408. // }
  409. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  410. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  411. // if foundYarn || foundNPM {
  412. // // it is safe to assume that the project contains a package.json
  413. // fileContent, _, err := client.RepositoryFiles.GetRawFile(
  414. // fmt.Sprintf("%s/%s", owner, name), fmt.Sprintf("%s/package.json", path),
  415. // &gitlab.GetRawFileOptions{
  416. // Ref: gitlab.String(ref),
  417. // },
  418. // )
  419. // if err != nil {
  420. // paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  421. // heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  422. // return fmt.Errorf("error fetching contents of package.json: %v", err)
  423. // }
  424. // var packageJSON struct {
  425. // Scripts map[string]string `json:"scripts"`
  426. // Engines struct {
  427. // Node string `json:"node"`
  428. // } `json:"engines"`
  429. // }
  430. // data := string(fileContent)
  431. // err = json.NewDecoder(strings.NewReader(data)).Decode(&packageJSON)
  432. // if err != nil {
  433. // paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  434. // heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  435. // return fmt.Errorf("error decoding package.json contents to struct: %v", err)
  436. // }
  437. // if packageJSON.Engines.Node == "" {
  438. // // we should now check for the node engine version in .nvmrc and then .node-version
  439. // nvmrcFound := false
  440. // nodeVersionFound := false
  441. // for i := 0; i < len(tree); i++ {
  442. // name := tree[i].Name
  443. // if name == ".nvmrc" {
  444. // nvmrcFound = true
  445. // } else if name == ".node-version" {
  446. // nodeVersionFound = true
  447. // }
  448. // }
  449. // if nvmrcFound {
  450. // // copy exact behavior of https://github.com/paketo-buildpacks/node-engine/blob/main/nvmrc_parser.go
  451. // fileContent, _, err = client.RepositoryFiles.GetRawFile(
  452. // fmt.Sprintf("%s/%s", owner, name), fmt.Sprintf("%s/.nvmrc", path),
  453. // &gitlab.GetRawFileOptions{
  454. // Ref: gitlab.String(ref),
  455. // },
  456. // )
  457. // if err != nil {
  458. // paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  459. // heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  460. // return fmt.Errorf("error fetching contents of .nvmrc: %v", err)
  461. // }
  462. // data = string(fileContent)
  463. // nvmrcVersion, err := validateNvmrc(data)
  464. // if err != nil {
  465. // paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  466. // heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  467. // return fmt.Errorf("error validating .nvmrc: %v", err)
  468. // }
  469. // nvmrcVersion = formatNvmrcContent(nvmrcVersion)
  470. // if nvmrcVersion != "*" {
  471. // packageJSON.Engines.Node = data
  472. // }
  473. // }
  474. // if packageJSON.Engines.Node == "" && nodeVersionFound {
  475. // // copy exact behavior of https://github.com/paketo-buildpacks/node-engine/blob/main/node_version_parser.go
  476. // fileContent, _, err = client.RepositoryFiles.GetRawFile(
  477. // fmt.Sprintf("%s/%s", owner, name), fmt.Sprintf("%s/.node-version", path),
  478. // &gitlab.GetRawFileOptions{
  479. // Ref: gitlab.String(ref),
  480. // },
  481. // )
  482. // if err != nil {
  483. // paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  484. // heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  485. // return fmt.Errorf("error fetching contents of .node-version: %v", err)
  486. // }
  487. // data = string(fileContent)
  488. // nodeVersion, err := validateNodeVersion(data)
  489. // if err != nil {
  490. // paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  491. // heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  492. // return fmt.Errorf("error validating .node-version: %v", err)
  493. // }
  494. // if nodeVersion != "" {
  495. // packageJSON.Engines.Node = nodeVersion
  496. // }
  497. // }
  498. // }
  499. // if packageJSON.Engines.Node == "" {
  500. // // use the default node engine version from https://github.com/paketo-buildpacks/node-engine/blob/main/buildpack.toml
  501. // packageJSON.Engines.Node = "16.*.*"
  502. // }
  503. // paketoBuildpackInfo.Config = make(map[string]interface{})
  504. // paketoBuildpackInfo.Config["scripts"] = packageJSON.Scripts
  505. // paketoBuildpackInfo.Config["node_engine"] = packageJSON.Engines.Node
  506. // paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  507. // herokuBuildpackInfo.Config = make(map[string]interface{})
  508. // herokuBuildpackInfo.Config["scripts"] = packageJSON.Scripts
  509. // herokuBuildpackInfo.Config["node_engine"] = packageJSON.Engines.Node
  510. // heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  511. // } else if foundStandalone {
  512. // paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  513. // heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  514. // }
  515. return nil
  516. }