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