|
| 1 | +// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by the license in the LICENSE file. |
| 3 | + |
| 4 | +package planner |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "go.jetpack.io/devbox/cuecfg" |
| 11 | +) |
| 12 | + |
| 13 | +type NodeJSPlanner struct{} |
| 14 | + |
| 15 | +// NodeJsPlanner implements interface Planner (compile-time check) |
| 16 | +var _ Planner = (*NodeJSPlanner)(nil) |
| 17 | + |
| 18 | +func (n *NodeJSPlanner) Name() string { |
| 19 | + return "NodeJsPlanner" |
| 20 | +} |
| 21 | + |
| 22 | +func (n *NodeJSPlanner) IsRelevant(srcDir string) bool { |
| 23 | + packageJSONPath := filepath.Join(srcDir, "package.json") |
| 24 | + return fileExists(packageJSONPath) |
| 25 | +} |
| 26 | + |
| 27 | +func (n *NodeJSPlanner) GetPlan(srcDir string) *Plan { |
| 28 | + packages := []string{n.nodePackage(srcDir)} |
| 29 | + pkgManager := "npm" |
| 30 | + inputFiles := []string{ |
| 31 | + filepath.Join(srcDir, "package.json"), |
| 32 | + } |
| 33 | + |
| 34 | + npmPkgLockPath := filepath.Join(srcDir, "package-lock.json") |
| 35 | + if fileExists(npmPkgLockPath) { |
| 36 | + inputFiles = append(inputFiles, npmPkgLockPath) |
| 37 | + } |
| 38 | + |
| 39 | + yarnPkgLockPath := filepath.Join(srcDir, "yarn.lock") |
| 40 | + if fileExists(yarnPkgLockPath) { |
| 41 | + pkgManager = "yarn" |
| 42 | + packages = append(packages, "yarn") |
| 43 | + inputFiles = append(inputFiles, yarnPkgLockPath) |
| 44 | + } |
| 45 | + |
| 46 | + return &Plan{ |
| 47 | + DevPackages: packages, |
| 48 | + // TODO: Optimize runtime packages to remove npm or yarn if startStage command use Node directly. |
| 49 | + RuntimePackages: packages, |
| 50 | + |
| 51 | + SharedPlan: SharedPlan{ |
| 52 | + InstallStage: &Stage{ |
| 53 | + InputFiles: inputFiles, |
| 54 | + Command: fmt.Sprintf("%s install", pkgManager), |
| 55 | + }, |
| 56 | + |
| 57 | + BuildStage: &Stage{ |
| 58 | + // Copy the rest of the directory over, since at install stage we only copied package.json and its lock file. |
| 59 | + InputFiles: []string{"."}, |
| 60 | + // Command: "" (command should be set by users. Some apps don't require a build command.) |
| 61 | + }, |
| 62 | + |
| 63 | + StartStage: &Stage{ |
| 64 | + // Start command could be `Node server.js`, `npm serve`, `yarn start`, or anything really. |
| 65 | + // For now we use `node index.js` as the default. |
| 66 | + Command: "node index.js", |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +type nodeProject struct { |
| 73 | + Engines struct { |
| 74 | + Node string `json:"node,omitempty"` |
| 75 | + } `json:"engines,omitempty"` |
| 76 | +} |
| 77 | + |
| 78 | +func (n *NodeJSPlanner) nodePackage(srcDir string) string { |
| 79 | + v := n.nodeVersion(srcDir) |
| 80 | + if v != nil { |
| 81 | + switch v.major() { |
| 82 | + case "10": |
| 83 | + return "nodejs-10_x" |
| 84 | + case "12": |
| 85 | + return "nodejs-12_x" |
| 86 | + case "16": |
| 87 | + return "nodejs-16_x" |
| 88 | + case "18": |
| 89 | + return "nodejs-18_x" |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return "nodejs" |
| 94 | +} |
| 95 | + |
| 96 | +func (n *NodeJSPlanner) nodeVersion(srcDir string) *version { |
| 97 | + p := n.nodeProject(srcDir) |
| 98 | + if p != nil { |
| 99 | + if v, err := newVersion(p.Engines.Node); err == nil { |
| 100 | + return v |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func (n *NodeJSPlanner) nodeProject(srcDir string) *nodeProject { |
| 108 | + packageJSONPath := filepath.Join(srcDir, "package.json") |
| 109 | + p := &nodeProject{} |
| 110 | + _ = cuecfg.ReadFile(packageJSONPath, p) |
| 111 | + |
| 112 | + return p |
| 113 | +} |
0 commit comments