@@ -29,22 +29,45 @@ def check_node_version() -> bool:
2929 )
3030
3131
32- @once
33- def get_node_version () -> version .Version | None :
34- """Get the version of node.
32+ def _get_version_of_executable (
33+ executable_path : Path | None , version_arg : str = "--version"
34+ ) -> version .Version | None :
35+ """Get the version of an executable.
36+
37+ Args:
38+ executable_path: The path to the executable.
39+ version_arg: The argument to pass to the executable to get its version.
3540
3641 Returns:
37- The version of node .
42+ The version of the executable .
3843 """
39- node_path = path_ops .get_node_path ()
40- if node_path is None :
44+ if executable_path is None :
4145 return None
4246 try :
43- result = processes .new_process ([node_path , "-v" ], run = True )
44- # The output will be in the form "vX.Y.Z", but version.parse() can handle it
45- return version .parse (result .stdout )
47+ result = processes .new_process ([executable_path , version_arg ], run = True )
48+ if result .returncode != 0 :
49+ console .error (
50+ f"Failed to run { executable_path } { version_arg } to get version. Return code: { result .returncode } . Standard error: { result .stderr !r} ."
51+ )
52+ return None
53+ return version .parse (result .stdout .strip ())
4654 except (FileNotFoundError , TypeError ):
4755 return None
56+ except version .InvalidVersion as e :
57+ console .warn (
58+ f"The detected version of { executable_path } ({ e .args [0 ]} ) is not valid. Defaulting to None."
59+ )
60+ return None
61+
62+
63+ @once
64+ def get_node_version () -> version .Version | None :
65+ """Get the version of node.
66+
67+ Returns:
68+ The version of node.
69+ """
70+ return _get_version_of_executable (path_ops .get_node_path ())
4871
4972
5073def get_bun_version (bun_path : Path | None = None ) -> version .Version | None :
@@ -56,20 +79,7 @@ def get_bun_version(bun_path: Path | None = None) -> version.Version | None:
5679 Returns:
5780 The version of bun.
5881 """
59- bun_path = bun_path or path_ops .get_bun_path ()
60- if bun_path is None :
61- return None
62- try :
63- # Run the bun -v command and capture the output
64- result = processes .new_process ([str (bun_path ), "-v" ], run = True )
65- return version .parse (str (result .stdout ))
66- except FileNotFoundError :
67- return None
68- except version .InvalidVersion as e :
69- console .warn (
70- f"The detected bun version ({ e .args [0 ]} ) is not valid. Defaulting to None."
71- )
72- return None
82+ return _get_version_of_executable (bun_path or path_ops .get_bun_path ())
7383
7484
7585def npm_escape_hatch () -> bool :
0 commit comments