@@ -13,18 +13,24 @@ local M = {
1313--- @return nil | string node_version_error
1414function M .get_node_version ()
1515 if not M .node_version then
16- local cmd = { M .node_command , " --version" }
17- local cmd_output_table = vim .fn .executable (M .node_command ) == 1 and vim .fn .systemlist (cmd , nil , 0 ) or { " " }
18- local cmd_output = cmd_output_table [# cmd_output_table ]
19- local cmd_exit_code = vim .v .shell_error
16+ local version_cmd = vim .split (M .node_command , " " )
17+ table.insert (version_cmd , " --version" )
18+
2019 local node_version_major = 0
2120 local node_version = " "
22-
23- if cmd_output then
24- node_version = string.match (cmd_output , " ^v(%S+)" ) or node_version
25- node_version_major = tonumber (string.match (node_version , " ^(%d+)%." )) or node_version_major
26- else
27- cmd_output = " [no output]"
21+ local cmd_exit_code = - 1
22+ local cmd_output = " [no output]"
23+ local ok , process = pcall (vim .system , version_cmd )
24+
25+ if ok and process then
26+ local result = process :wait ()
27+ cmd_output = result .stdout or cmd_output
28+ cmd_exit_code = result .code
29+
30+ if cmd_output and cmd_output ~= " [no output]" then
31+ node_version = string.match (cmd_output , " ^v(%S+)" ) or node_version
32+ node_version_major = tonumber (string.match (node_version , " ^(%d+)%." )) or node_version_major
33+ end
2834 end
2935
3036 if node_version_major == 0 then
@@ -63,17 +69,6 @@ function M.validate_node_version()
6369 return true
6470end
6571
66- function M .node_exists ()
67- local node_exists = vim .fn .executable (M .node_command ) == 1
68-
69- if not node_exists then
70- logger .error (" node.js is not installed or not in PATH" )
71- return false
72- end
73-
74- return true
75- end
76-
7772--- @param server_path ? string
7873--- @return boolean
7974function M .init_agent_path (server_path )
10196
10297--- @return table
10398function M .get_execute_command ()
104- return {
105- M .node_command ,
106- M .server_path or M .get_server_path (),
107- " --stdio" ,
108- }
99+ local cmd = vim .split (M .node_command , " " )
100+ table.insert (cmd , M .server_path or M .get_server_path ())
101+ table.insert (cmd , " --stdio" )
102+ return cmd
109103end
110104
111105--- @param node_command ? string
114108function M .setup (node_command , custom_server_path )
115109 M .node_command = node_command or " node"
116110
117- if not M .node_exists () or not M . validate_node_version () or not M .init_agent_path (custom_server_path ) then
111+ if not M .validate_node_version () or not M .init_agent_path (custom_server_path ) then
118112 return false
119113 end
120114
0 commit comments