Skip to content

Commit

Permalink
revert(planner/nodejs): Implement multi-layer build for Node.js (#418)
Browse files Browse the repository at this point in the history
#### Description (required)

There are some modules that needs the source code tree. Therefore, we
will not only copy `.medusa/server` now.

This reverts commit 4c59e19.

#### Related issues & labels (optional)

- Experiments
- Suggested label: enhancement
  • Loading branch information
yuaanlin authored Jan 25, 2025
2 parents a3152da + 5e368dd commit c253d40
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 83 deletions.
26 changes: 11 additions & 15 deletions internal/nodejs/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ type TemplateContext struct {

AppDir string

InstallCmd string
BuildCmd string
BuildRuntimeCmd string // currently only used by Medusa
StartCmd string
RuntimeBaseDir string // currently only used by Medusa
InstallCmd string
BuildCmd string
StartCmd string

Framework string
Serverless bool
Expand Down Expand Up @@ -53,16 +51,14 @@ func (c TemplateContext) Execute() (string, error) {

func getContextBasedOnMeta(meta types.PlanMeta) TemplateContext {
context := TemplateContext{
NodeVersion: meta["nodeVersion"],
AppDir: meta["appDir"],
InstallCmd: meta["installCmd"],
BuildCmd: meta["buildCmd"],
BuildRuntimeCmd: meta["buildRuntimeCmd"],
StartCmd: meta["startCmd"],
RuntimeBaseDir: meta["runtimeBaseDir"],
Framework: meta["framework"],
Serverless: meta["serverless"] == "true",
OutputDir: meta["outputDir"],
NodeVersion: meta["nodeVersion"],
AppDir: meta["appDir"],
InstallCmd: meta["installCmd"],
BuildCmd: meta["buildCmd"],
StartCmd: meta["startCmd"],
Framework: meta["framework"],
Serverless: meta["serverless"] == "true",
OutputDir: meta["outputDir"],

// The flag specific to planner/bun.
Bun: meta["bun"] == "true" || meta["packageManager"] == "bun",
Expand Down
20 changes: 0 additions & 20 deletions internal/nodejs/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,3 @@ func TestGetContextBasedOnMeta_WithOutputdirAndMPAFramework(t *testing.T) {
OutputDir: "dist",
})
}

func TestGetContextBasedOnMeta_RuntimeEnvironment(t *testing.T) {
meta := getContextBasedOnMeta(types.PlanMeta{
"nodeVersion": "16",
"installCmd": "RUN npm install",
"buildCmd": "npm run build",
"buildRuntimeCmd": "npm run build-runtime",
"startCmd": "npm run start",
"runtimeBaseDir": "/src/.output",
})

assert.Equal(t, meta, TemplateContext{
NodeVersion: "16",
InstallCmd: "RUN npm install",
BuildCmd: "npm run build",
BuildRuntimeCmd: "npm run build-runtime",
StartCmd: "npm run start",
RuntimeBaseDir: "/src/.output",
})
}
50 changes: 14 additions & 36 deletions internal/nodejs/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,42 +677,27 @@ func GetBuildCmd(ctx *nodePlanContext) string {
}
}

*cmd = optional.Some(buildCmd)
return cmd.Unwrap()
}

// GetRuntimeBaseDir gets the base directory of the runtime environment of the Node.js app.
func GetRuntimeBaseDir(ctx *nodePlanContext) string {
framework := DetermineAppFramework(ctx)

if framework == types.NodeProjectFrameworkMedusa {
return "/src/.medusa/server"
}

return ""
}

// GetBuildRuntimeCmd gets the build command to build the runtime environment of the Node.js app.
func GetBuildRuntimeCmd(ctx *nodePlanContext) string {
pkgManager := DeterminePackageManager(ctx)
framework := DetermineAppFramework(ctx)

if framework == types.NodeProjectFrameworkMedusa {
var installCmd string
switch pkgManager {
case types.NodePackageManagerNpm:
return "npm install"
installCmd = "npm install"
case types.NodePackageManagerPnpm:
return "pnpm install"
installCmd = "pnpm install"
case types.NodePackageManagerYarn:
return "yarn install"
installCmd = "yarn install"
case types.NodePackageManagerBun:
return "bun install"
installCmd = "bun install"
default:
return "yarn install"
installCmd = "yarn install"
}

// Install the dependencies in ".medusa/server" directory.
buildCmd += " && " + "cd .medusa/server" + " && " + installCmd
}

return ""
*cmd = optional.Some(buildCmd)
return cmd.Unwrap()
}

// GetMonorepoAppRoot gets the app root of the monorepo project in the Node.js project.
Expand Down Expand Up @@ -852,6 +837,9 @@ func GetStartCmd(ctx *nodePlanContext) string {
if predeployScript != "" {
startCmd = GetScriptCommand(ctx, predeployScript) + " && " + startCmd
}
if framework == types.NodeProjectFrameworkMedusa {
startCmd = "cd .medusa/server" + " && " + startCmd
}

*cmd = optional.Some(startCmd)
return cmd.Unwrap()
Expand Down Expand Up @@ -1094,16 +1082,6 @@ func GetMeta(opt GetMetaOptions) types.PlanMeta {
startCmd := GetStartCmd(ctx)
meta["startCmd"] = startCmd

runtimeBaseDir := GetRuntimeBaseDir(ctx)
if runtimeBaseDir != "" {
meta["runtimeBaseDir"] = runtimeBaseDir
}

buildRuntimeCmd := GetBuildRuntimeCmd(ctx)
if buildRuntimeCmd != "" {
meta["buildRuntimeCmd"] = buildRuntimeCmd
}

// only set outputDir if there is no start command
// (because if there is, it shouldn't be a static project)
if startCmd == "" {
Expand Down
8 changes: 0 additions & 8 deletions internal/nodejs/templates/template.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ COPY --from=build /src/{{ .AppDir }}/{{ .OutputDir }} /
FROM zeabur/caddy-static AS runtime
COPY --from=output / /usr/share/caddy
{{ end }}
{{ else if ne .RuntimeBaseDir "" }}
FROM build AS prod
COPY --from=build {{ .RuntimeBaseDir }} /app/
WORKDIR /app

EXPOSE 8080
RUN {{ .BuildRuntimeCmd }}
CMD {{ .StartCmd }}
{{ else }}
EXPOSE 8080
CMD {{ .StartCmd }}{{ end }}
5 changes: 5 additions & 0 deletions tests/real_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ var projects = []struct {
owner: "medusajs",
repo: "medusa-starter-default",
},
{
name: "nodejs-medusa-zb",
owner: "zeabur",
repo: "medusa-starter-default",
},
{
name: "nodejs-a-lot-of-dependencies",
dir: "nodejs-a-lot-of-dependencies",
Expand Down
11 changes: 11 additions & 0 deletions tests/snapshots/nodejs-medusa-zb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PlanType: nodejs

Meta:
appDir: ""
buildCmd: "yarn build && cd .medusa/server && yarn install"
bun: "false"
framework: "medusa"
installCmd: "COPY . .\nRUN yarn install"
nodeVersion: "20"
packageManager: "yarn"
startCmd: "cd .medusa/server && yarn predeploy && yarn start"
6 changes: 2 additions & 4 deletions tests/snapshots/nodejs-medusa.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ PlanType: nodejs

Meta:
appDir: ""
buildCmd: "yarn build"
buildRuntimeCmd: "yarn install"
buildCmd: "yarn build && cd .medusa/server && yarn install"
bun: "false"
framework: "medusa"
installCmd: "COPY . .\nRUN yarn install"
nodeVersion: "20"
packageManager: "yarn"
runtimeBaseDir: "/src/.medusa/server"
startCmd: "yarn start"
startCmd: "cd .medusa/server && yarn start"

0 comments on commit c253d40

Please sign in to comment.