diff --git a/README.md b/README.md index b8703ec..a4ec879 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ AutoDeploy 是一个面向 Windows 的 Spring Boot / SSM + Vue 本地自动化 - 服务启动前检查端口占用并终止旧进程,等待新服务端口就绪后再继续。 - 后端在独立控制台中显示实时输出,各服务同时保留运行日志。 - Maven、npm 等命令输出会自动识别 UTF-8 和 Windows 中文编码,避免日志解码导致部署任务提前中断。 +- 使用手动指定或工具自动安装的 npm 时,会把其 Node.js 目录加入部署子进程的 `PATH`,保证依赖安装脚本可以调用 `node`。 ## 运行环境 diff --git a/deploy_tool/deployer.py b/deploy_tool/deployer.py index 4995fcf..fe38eca 100644 --- a/deploy_tool/deployer.py +++ b/deploy_tool/deployer.py @@ -831,12 +831,14 @@ def _run_process( input_file = _open_utf8_sql_input(input_path, on_output) if input_path else None try: + process_environment = _build_process_environment(command) process = subprocess.Popen( command, cwd=cwd, stdin=input_file, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + env=process_environment, shell=False, ) assert process.stdout is not None @@ -848,6 +850,18 @@ def _run_process( input_file.close() +def _build_process_environment(command: list[str]) -> dict[str, str]: + environment = os.environ.copy() + executable = Path(command[0]) if command else None + if executable and executable.parent != Path("."): + executable_dir = str(executable.parent) + current_path = environment.get("PATH", "") + environment["PATH"] = os.pathsep.join( + part for part in (executable_dir, current_path) if part + ) + return environment + + def _decode_process_output(value: bytes | str) -> str: if isinstance(value, str): return value diff --git a/tests/test_deployer.py b/tests/test_deployer.py index 2c51a26..faac785 100644 --- a/tests/test_deployer.py +++ b/tests/test_deployer.py @@ -14,6 +14,7 @@ class DeployerTests(unittest.TestCase): def test_process_output_accepts_utf8_and_gbk_without_decode_failure(self): output = [] popen_options = {} + npm = Path.cwd() / "fake-node" / "npm.cmd" class FakeProcess: stdout = iter( @@ -32,7 +33,7 @@ class DeployerTests(unittest.TestCase): with patch.object(deployer_module.subprocess, "Popen", side_effect=fake_popen): exit_code = deployer_module._run_process( - ["npm.cmd", "install"], + [str(npm), "install"], Path("C:/project/web"), output.append, ) @@ -40,6 +41,10 @@ class DeployerTests(unittest.TestCase): self.assertEqual(exit_code, 1) self.assertEqual(output, ["npm ERR! ⚠ dependency failed", "安装失败"]) self.assertNotIn("text", popen_options) + self.assertEqual( + Path(popen_options["env"]["PATH"].split(os.pathsep)[0]), + npm.parent, + ) def test_frozen_app_reuses_executable_for_service_console(self): with patch.object(deployer_module.sys, "frozen", True, create=True):