2
0

nodejs.go 16 KB

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