fix: node env path loading
This commit is contained in:
@@ -78,9 +78,6 @@ log " - node: $(node --version 2>&1 | head -n 1 || echo "not found")"
|
|||||||
log "Checking npm..."
|
log "Checking npm..."
|
||||||
log " - npm: $(npm --version 2>&1 | head -n 1 || echo "not found")"
|
log " - npm: $(npm --version 2>&1 | head -n 1 || echo "not found")"
|
||||||
|
|
||||||
#export NODE_PATH=$(npm root -g 2>/dev/null || echo "")
|
|
||||||
#log " - node_path: $NODE_PATH"
|
|
||||||
|
|
||||||
# ============================
|
# ============================
|
||||||
# 将 baihu 注册到全局命令
|
# 将 baihu 注册到全局命令
|
||||||
# ============================
|
# ============================
|
||||||
|
|||||||
@@ -113,6 +113,18 @@ func ExecuteWithHooks(ctx context.Context, req Request, stdout, stderr io.Writer
|
|||||||
execCtx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Minute)
|
execCtx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
// 隐式且静默地获取包含 node 环境的全局路径,并注入环境变量 (带极速缓存)
|
||||||
|
if req.UseMise {
|
||||||
|
for _, lang := range req.Languages {
|
||||||
|
if lang["name"] == "node" {
|
||||||
|
if nodePath := utils.GetMiseNodePath(lang["version"]); nodePath != "" {
|
||||||
|
req.Envs = append(req.Envs, "NODE_PATH="+nodePath)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 如果指定使用 mise,则预先构建好带 mise 的命令,这样 PreExecute 记录的就是完整命令
|
// 如果指定使用 mise,则预先构建好带 mise 的命令,这样 PreExecute 记录的就是完整命令
|
||||||
if req.UseMise {
|
if req.UseMise {
|
||||||
req.Command = utils.BuildMiseCommand(req.Command, req.Languages)
|
req.Command = utils.BuildMiseCommand(req.Command, req.Languages)
|
||||||
|
|||||||
@@ -1,9 +1,37 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var nodePathCache sync.Map
|
||||||
|
|
||||||
|
// GetMiseNodePath 获取指定版本的 node 全局包路径,使用内存缓存避免重复获取
|
||||||
|
func GetMiseNodePath(version string) string {
|
||||||
|
if version == "" {
|
||||||
|
version = "latest"
|
||||||
|
}
|
||||||
|
|
||||||
|
if val, ok := nodePathCache.Load(version); ok {
|
||||||
|
return val.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("mise", "where", "node@"+version)
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err == nil {
|
||||||
|
nodeDir := strings.TrimSpace(string(out))
|
||||||
|
if nodeDir != "" {
|
||||||
|
nodePath := nodeDir + "/lib/node_modules"
|
||||||
|
nodePathCache.Store(version, nodePath)
|
||||||
|
return nodePath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// BuildMiseCommand 构建多语言 mise 执行命令 (字符串形式)
|
// BuildMiseCommand 构建多语言 mise 执行命令 (字符串形式)
|
||||||
func BuildMiseCommand(command string, languages []map[string]string) string {
|
func BuildMiseCommand(command string, languages []map[string]string) string {
|
||||||
if len(languages) == 0 {
|
if len(languages) == 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user