core/providers/node/cleanse.go
core/providers/node/cleanse.goBrowse 1970 files
424 tokens
1,555 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1// there are certain commands which `rm -rf node_modules`, which will cause a cache checksum docker error.2// this cleanse operation imperfectly detects this case and removes node_modules from the cache keys to avoid this common user error.3// more info here: https://github.com/railwayapp/railpack/pull/2594 5package node6 7import (8 "regexp"9 10 "github.com/railwayapp/railpack/core/plan"11)12 13var (14 npmCiCommandRegex = regexp.MustCompile(`(?i)\bnpm\s+ci\b`)15 removeNodeModulesRegex = regexp.MustCompile(`(?i)\b(?:rm\s+-r[f]?|rmdir|rimraf)\s+(?:\S*\/)?node_modules\b`)16)17 18func willRemoveNodeModules(commands []plan.Command) bool {19 for _, cmd := range commands {20 if execCmd, ok := cmd.(plan.ExecCommand); ok {21 if npmCiCommandRegex.MatchString(execCmd.Cmd) || removeNodeModulesRegex.MatchString(execCmd.Cmd) {22 return true23 }24 }25 }26 return false27}28 29func (p *NodeProvider) CleansePlan(buildPlan *plan.BuildPlan) {30 var nodeModulesCacheKey string31 for cacheName, cacheDef := range buildPlan.Caches {32 if cacheDef.Directory == NODE_MODULES_CACHE {33 nodeModulesCacheKey = cacheName34 break35 }36 }37 38 if nodeModulesCacheKey == "" {39 return40 }41 42 for i, step := range buildPlan.Steps {43 if step.Name == "install" || !willRemoveNodeModules(step.Commands) {44 continue45 }46 47 before := len(step.Caches)48 if before == 0 {49 continue50 }51 52 var newCaches []string53 for _, name := range step.Caches {54 if name != "" && name != nodeModulesCacheKey {55 newCaches = append(newCaches, name)56 }57 }58 59 buildPlan.Steps[i].Caches = newCaches60 }61}62