Skip to content

Commit ff5cd32

Browse files
author
Marvin Zhang
committed
refactor: streamline Node.js path configuration in task runner
- Removed redundant home directory retrieval and nvm checks in the configureNodePath method. - Introduced a new utility function GetNodeModulesPath to centralize the logic for determining the global node_modules path. - Updated environment variable setup to use the new utility function, improving clarity and maintainability of the code.
1 parent a585ab1 commit ff5cd32

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

core/task/handler/runner.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -342,36 +342,15 @@ func (r *Runner) startHealthCheck() {
342342

343343
// configureNodePath sets up the Node.js environment paths, handling both nvm and default installations
344344
func (r *Runner) configureNodePath() {
345-
// Get user's home directory
346-
home, err := os.UserHomeDir()
347-
if err != nil {
348-
r.Errorf("error getting user home directory: %v", err)
349-
home = "/root" // fallback to root if it can't get home dir
350-
}
351-
352345
// Configure nvm-based Node.js paths
353346
envPath := os.Getenv("PATH")
354-
nvmPath := filepath.Join(home, ".nvm/versions/node")
355-
356-
// Check if nvm is being used
357-
if utils.Exists(nvmPath) {
358-
// Get the current node version from NVM
359-
currentVersion := os.Getenv("NVM_BIN")
360-
if currentVersion != "" {
361-
nodePath := filepath.Dir(currentVersion) + "/lib/node_modules"
362-
if !strings.Contains(envPath, nodePath) {
363-
_ = os.Setenv("PATH", nodePath+":"+envPath)
364-
}
365-
_ = os.Setenv("NODE_PATH", nodePath)
366-
}
367-
} else {
368-
// Fallback to default global node_modules path
369-
nodePath := "/usr/lib/node_modules"
370-
if !strings.Contains(envPath, nodePath) {
371-
_ = os.Setenv("PATH", nodePath+":"+envPath)
372-
}
373-
_ = os.Setenv("NODE_PATH", nodePath)
347+
348+
// Configure global node_modules path
349+
nodePath := utils.GetNodeModulesPath()
350+
if !strings.Contains(envPath, nodePath) {
351+
_ = os.Setenv("PATH", nodePath+":"+envPath)
374352
}
353+
_ = os.Setenv("NODE_PATH", nodePath)
375354
}
376355

377356
// configureEnv sets up the environment variables for the task process, including:

core/utils/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
MetadataConfigDirName = ".crawlab"
3232
MetadataConfigName = "config.json"
3333
PyenvRoot = "/root/.pyenv"
34+
DefaultNodeModulesPath = "/usr/lib/node_modules"
3435
)
3536

3637
func IsDev() bool {
@@ -247,3 +248,10 @@ func GetInstallRoot() string {
247248
}
248249
return DefaultInstallRoot
249250
}
251+
252+
func GetNodeModulesPath() string {
253+
if res := viper.GetString("install.node.path"); res != "" {
254+
return res
255+
}
256+
return DefaultNodeModulesPath
257+
}

0 commit comments

Comments
 (0)