From 1662df1751fbf052f2a85c6bd5c8ddf346834473 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Tue, 12 Sep 2023 14:06:31 -0400 Subject: [PATCH] feat: better node version checking (#120) --- lua/sg/cody/rpc.lua | 5 +++-- lua/sg/health.lua | 6 ++++++ lua/sg/notify.lua | 9 ++------- lua/sg/utils.lua | 23 +++++++++++++++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lua/sg/cody/rpc.lua b/lua/sg/cody/rpc.lua index 9f54107a..1b6f08b0 100644 --- a/lua/sg/cody/rpc.lua +++ b/lua/sg/cody/rpc.lua @@ -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?)} diff --git a/lua/sg/health.lua b/lua/sg/health.lua index a26ca0fb..c6988116 100644 --- a/lua/sg/health.lua +++ b/lua/sg/health.lua @@ -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 diff --git a/lua/sg/notify.lua b/lua/sg/notify.lua index 382f0911..beaadc18 100644 --- a/lua/sg/notify.lua +++ b/lua/sg/notify.lua @@ -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 diff --git a/lua/sg/utils.lua b/lua/sg/utils.lua index c9b141c4..d6e5611b 100644 --- a/lua/sg/utils.lua +++ b/lua/sg/utils.lua @@ -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