Skip to content

Commit

Permalink
feat: better node version checking (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries authored Sep 12, 2023
1 parent 009feb0 commit 1662df1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lua/sg/cody/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ M.start = function(opts)
vim.wait(10)
end

if 1 ~= vim.fn.executable(config.node_executable) then
return require("sg.notify").INVALID_NODE(config.node_executable)
local ok, reason = require("sg.utils").valid_node_executable(config.node_executable)
if not ok then
return require("sg.notify").INVALID_NODE(reason)
end

---@type {["chat/updateMessageInProgress"]: fun(noti: CodyChatUpdateMessageInProgressNoti?)}
Expand Down
6 changes: 6 additions & 0 deletions lua/sg/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ end
local report_agent = function()
local config = require "sg.config"

local ok, reason = require("sg.utils").valid_node_executable(config.node_executable)
if not ok then
vim.health.error("Invalid node executable: " .. reason)
return false
end

if 1 ~= vim.fn.executable(config.node_executable) then
vim.health.error(string.format("config.node_executable (%s) not executable", config.node_executable))
return false
Expand Down
9 changes: 2 additions & 7 deletions lua/sg/notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ M.CODY_DISABLED = function()
vim.notify_once "[sg-cody] Cody is disabled for your current instance. Please talk to site-admins or change authentication"
end

M.INVALID_NODE = function(node_executable)
vim.notify_once(
string.format(
"[sg-cody] Invalid node configuration ('%s'). Please check your node configuration and restart nvim.",
node_executable
)
)
M.INVALID_NODE = function(reason)
vim.notify_once(string.format("[sg-cody] Invalid node configuration: '%s'", reason))

return nil
end
Expand Down
23 changes: 23 additions & 0 deletions lua/sg/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,27 @@ utils.json_or_nil = function(file)
return nil
end

utils.valid_node_executable = function(executable)
if 1 ~= vim.fn.executable(executable) then
return false, string.format("invalid executable: %s", executable)
end

local output = vim.fn.systemlist(executable .. " --version")[1]
local ok, version = pcall(vim.version.parse, output)
if not ok then
return false, string.format("invalid node version: %s", output)
end

if not version then
return false, string.format("unable to parse node version output: %s", output)
end

local min_node_version = assert(vim.version.parse "v18")
if not vim.version.gt(version, min_node_version) then
return false, string.format("node version must be >= %s. Got: %s", min_node_version, version)
end

return true
end

return utils

0 comments on commit 1662df1

Please sign in to comment.