Skip to content

Commit

Permalink
feat(planner/nodejs): Implement cache_dependencies switch
Browse files Browse the repository at this point in the history
Linear: ZEA-2097
  • Loading branch information
pan93412 committed Oct 27, 2023
1 parent 73b9e75 commit 973d551
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/nodejs/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (i *identify) PlanMeta(options plan.NewPlannerOptions) types.PlanMeta {
return GetMeta(
GetMetaOptions{
Src: options.Source,
Config: options.Config,
CustomBuildCmd: options.CustomBuildCommand,
CustomStartCmd: options.CustomStartCommand,
OutputDir: options.OutputDir,
Expand Down
3 changes: 2 additions & 1 deletion internal/nodejs/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type TemplateContext struct {
BuildCmd string
StartCmd string

Bun bool
CacheDependencies bool
Bun bool
}

//go:embed templates
Expand Down
31 changes: 29 additions & 2 deletions internal/nodejs/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ import (
"github.com/moznion/go-optional"
"github.com/spf13/afero"
"github.com/zeabur/zbpack/internal/utils"
"github.com/zeabur/zbpack/pkg/plan"
"github.com/zeabur/zbpack/pkg/types"
)

const (
// ConfigCacheDependencies is the key for the configuration of
// whether to cache dependencies.
// It should be true by default.
ConfigCacheDependencies = "node.cache_dependencies"
)

type nodePlanContext struct {
PackageJSON PackageJSON
Config plan.ImmutableProjectConfiguration
Src afero.Fs
Bun bool

Expand Down Expand Up @@ -439,7 +448,15 @@ func GetInstallCmd(ctx *nodePlanContext) string {
}

pkgManager := DeterminePackageManager(ctx)
cmds := []string{"COPY package.json* tsconfig.json* .npmrc* ."}
shouldCacheDependencies := getBoolWithDefault(ctx.Config, ConfigCacheDependencies, true)

var cmds []string
if shouldCacheDependencies {
cmds = append(cmds, "COPY package.json* tsconfig.json* .npmrc* .")
} else {
cmds = append(cmds, "COPY . .")
}

switch pkgManager {
case types.NodePackageManagerNpm:
cmds = append(cmds, "COPY package-lock.json* .", "RUN npm install")
Expand Down Expand Up @@ -623,7 +640,8 @@ func getServerless(ctx *nodePlanContext) bool {

// GetMetaOptions is the options for GetMeta.
type GetMetaOptions struct {
Src afero.Fs
Src afero.Fs
Config plan.ImmutableProjectConfiguration

CustomBuildCmd *string
CustomStartCmd *string
Expand All @@ -642,6 +660,7 @@ func GetMeta(opt GetMetaOptions) types.PlanMeta {

ctx := &nodePlanContext{
PackageJSON: packageJSON,
Config: opt.Config,
Src: opt.Src,
Bun: opt.Bun,
}
Expand Down Expand Up @@ -695,3 +714,11 @@ func GetMeta(opt GetMetaOptions) types.PlanMeta {

return meta
}

func getBoolWithDefault(config plan.ImmutableProjectConfiguration, key string, defaultValue bool) bool {
if !config.IsSet(key) {
return defaultValue
}

return config.GetBool(key)
}

0 comments on commit 973d551

Please sign in to comment.