Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lua/typescript-tools/tsserver_provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local is_win = vim.loop.os_uname().version:find "Windows"
---@field private instance TsserverProvider
---@field private callbacks function[]
---@field private root_dir Path
---@field private node_module_ancestor Path
---@field private npm_local_path Path
---@field private yarn_sdk_path Path
---@field private npm_global_path Path
Expand Down Expand Up @@ -67,7 +68,8 @@ function TsserverProvider.new(on_loaded)
local sanitized_bufname = vim.fs.normalize(bufname)

self.root_dir = Path:new(config.get_root_dir(sanitized_bufname, bufnr))
self.npm_local_path = find_deep_node_modules_ancestor(sanitized_bufname):joinpath "node_modules"
self.node_module_ancestor = find_deep_node_modules_ancestor(sanitized_bufname)
self.npm_local_path = self.node_module_ancestor:joinpath "node_modules"
self.yarn_sdk_path = find_yarn_sdk(sanitized_bufname):joinpath ".yarn/sdks"
self.global_install_path = Path:new(vim.fn.resolve(vim.fn.exepath "tsserver")):parent():parent()

Expand All @@ -88,14 +90,26 @@ end

---@private
---@return string, string[]
function TsserverProvider:make_npm_root_params() -- luacheck: ignore
function TsserverProvider:make_npm_root_params()
local args = { "root", "-g" }
local package_json_path = self.node_module_ancestor:joinpath "package.json"
local package_manager

if package_json_path:exists() then
-- pcall will handle cases where the file can't be read
local ok, package_json =
pcall(vim.json.decode, package_json_path:read(), { luanil = { object = true } })
if ok and package_json and package_json.packageManager then
package_manager = package_json.packageManager:match "^(%w+)"
end
end

local pm = package_manager or "npm"
if is_win then
return "cmd.exe", { "/c", "npm", unpack(args) }
return "cmd.exe", { "/c", pm, unpack(args) }
end

return "npm", args
return pm, args
end

---@param on_loaded function
Expand Down