From 7310df1be6abce945f4e98680bb5bd0e6f947464 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Fri, 3 Jan 2020 14:21:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9Node.js=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/services/task.go | 12 ++++ .../components/InfoView/SpiderInfoView.vue | 8 +-- .../src/components/Node/NodeInstallation.vue | 69 ++++++++++++++++--- frontend/src/i18n/zh.js | 6 +- 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/backend/services/task.go b/backend/services/task.go index 9e6fdbc8..c0aa8fc8 100644 --- a/backend/services/task.go +++ b/backend/services/task.go @@ -19,6 +19,7 @@ import ( "runtime" "runtime/debug" "strconv" + "strings" "sync" "syscall" "time" @@ -104,6 +105,17 @@ func AssignTask(task model.Task) error { // 设置环境变量 func SetEnv(cmd *exec.Cmd, envs []model.Env, taskId string, dataCol string) *exec.Cmd { + // 默认把Node.js的全局node_modules加入环境变量 + envPath := os.Getenv("PATH") + for _, _path := range strings.Split(envPath, ":") { + if strings.Contains(_path, "/.nvm/versions/node/") { + pathNodeModules := strings.Replace(_path, "/bin", "/lib/node_modules", -1) + _ = os.Setenv("PATH", pathNodeModules+":"+envPath) + _ = os.Setenv("NODE_PATH", pathNodeModules) + break + } + } + // 默认环境变量 cmd.Env = append(os.Environ(), "CRAWLAB_TASK_ID="+taskId) cmd.Env = append(cmd.Env, "CRAWLAB_COLLECTION="+dataCol) diff --git a/frontend/src/components/InfoView/SpiderInfoView.vue b/frontend/src/components/InfoView/SpiderInfoView.vue index a13b4c7a..1f29e89c 100644 --- a/frontend/src/components/InfoView/SpiderInfoView.vue +++ b/frontend/src/components/InfoView/SpiderInfoView.vue @@ -51,6 +51,10 @@ + + {{$t('Run')}} + - - {{$t('Run')}} - {{$t('Save')}} diff --git a/frontend/src/components/Node/NodeInstallation.vue b/frontend/src/components/Node/NodeInstallation.vue index cf204da8..7cf4f6b9 100644 --- a/frontend/src/components/Node/NodeInstallation.vue +++ b/frontend/src/components/Node/NodeInstallation.vue @@ -3,6 +3,7 @@ + - +