diff --git a/docker/mise-hook.sh b/docker/mise-hook.sh index 37969ef..afcd500 100644 --- a/docker/mise-hook.sh +++ b/docker/mise-hook.sh @@ -1,22 +1,37 @@ #!/bin/bash # 面向终端的交互式 Hook 拦截器 -# 目的是为了在终端手动执行 `mise exec node -- node xxx` 时也能具有与 Go 系统底层相同的 NODE_PATH 环境 +# 目的是为了在终端执行 node 相关任务时具有与 Go 系统底层相同的 NODE_PATH 环境 +# +# 支持以下各种执行场景: +# 1. mise exec node@23.11.1 -- node index.js (指明具体版本) +# 2. mise exec python@3 node@23 -- node index.js (混合语言注入) +# 3. mise exec node -- node index.js (显式指定环境但使用默认版本) +# 4. mise exec -- node index.js (完全隐式环境,由项目配置决定) mise() { if [[ "$1" == "exec" || "$1" == "x" ]]; then - local is_node=false + local node_spec="" + # 优先从命令行参数中提取指定的 node 版本(如 node@23...) for arg in "$@"; do - if [[ "$arg" == *"node"* ]]; then - is_node=true + if [[ "$arg" == "--" ]]; then + break + elif [[ "$arg" == "node" || "$arg" == node@* ]]; then + node_spec="$arg" break fi done - if [[ "$is_node" == true ]]; then - local node_bin=$(command mise which node 2>/dev/null) - if [[ -n "$node_bin" && "$node_bin" == *"/bin/node" ]]; then - # 截取路径并附加到当前进程执行环境的前置变量 - NODE_PATH="${node_bin%/bin/node}/lib/node_modules" command mise "$@" + # 如果命令行参数中没写,则尝试检测该环境下是否有已激活的默认 node + if [[ -z "$node_spec" ]]; then + if command mise which node >/dev/null 2>&1; then + node_spec="node" + fi + fi + + if [[ -n "$node_spec" ]]; then + local node_dir=$(command mise where "$node_spec" 2>/dev/null) + if [[ -n "$node_dir" ]]; then + NODE_PATH="$node_dir/lib/node_modules" command mise "$@" return $? fi fi