Skip to content

Commit

Permalink
feat(astro): use vendored tsdk if workspace tsdk not available (#75)
Browse files Browse the repository at this point in the history
Closes #74.
  • Loading branch information
williamboman committed Oct 11, 2022
1 parent 0eb7cfe commit e492f12
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
10 changes: 10 additions & 0 deletions lua/mason-lspconfig/server_configurations/astro/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local typescript = require "mason-lspconfig.typescript"

---@param install_dir string
return function(install_dir)
return {
on_new_config = function(new_config, workspace_dir)
new_config.init_options.typescript.serverPath = typescript.resolve_server_path(install_dir, workspace_dir)
end,
}
end
25 changes: 5 additions & 20 deletions lua/mason-lspconfig/server_configurations/volar/init.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
local fs = require "mason-core.fs"
local path = require "mason-core.path"
local typescript = require "mason-lspconfig.typescript"

---@param install_dir string
return function(install_dir)
---@param dir string
local function get_tsserverlib_path(dir)
return path.concat { dir, "node_modules", "typescript", "lib" }
end

---@param workspace_dir string|nil
local function get_typescript_server_path(workspace_dir)
local local_tsserverlib = workspace_dir ~= nil and get_tsserverlib_path(workspace_dir)
local vendored_tsserverlib = get_tsserverlib_path(install_dir)
if local_tsserverlib and fs.sync.file_exists(local_tsserverlib) then
return local_tsserverlib
else
return vendored_tsserverlib
end
end

return {
on_new_config = function(new_config, new_install_dir)
new_config.init_options.typescript.tsdk = get_typescript_server_path(new_install_dir)
on_new_config = function(new_config, workspace_dir)
local tsdk = typescript.resolve_server_path(install_dir, workspace_dir)
new_config.init_options.typescript.serverPath = tsdk
new_config.init_options.typescript.tsdk = tsdk
end,
}
end
39 changes: 39 additions & 0 deletions lua/mason-lspconfig/typescript.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local _ = require "mason-core.functional"
local fs = require "mason-core.fs"
local path = require "mason-core.path"
local log = require "mason-core.log"

local typescript = {}

---@param lib_dir string
function typescript.find_typescript_module_in_lib(lib_dir)
return _.find_first(fs.sync.file_exists, {
path.concat { lib_dir, "tsserverlibrary.js" },
path.concat { lib_dir, "typescript.js" },
path.concat { lib_dir, "tsserver.js" },
})
end

---@param dir string
function typescript.tsdk(dir)
return path.concat { dir, "node_modules", "typescript", "lib" }
end

---@param package_dir string The Mason package installation directory where a vendored Typescript installation can be found.
---@param workspace_dir string
---@return string
function typescript.resolve_server_path(package_dir, workspace_dir)
local local_tsserverlib = workspace_dir ~= nil
and typescript.find_typescript_module_in_lib(typescript.tsdk(workspace_dir))
if local_tsserverlib then
return local_tsserverlib
else
local vendored_tsserverlib = typescript.find_typescript_module_in_lib(typescript.tsdk(package_dir))
if not vendored_tsserverlib then
log.fmt_error("Failed to find vendored Typescript module in %s", package_dir)
end
return vendored_tsserverlib
end
end

return typescript

0 comments on commit e492f12

Please sign in to comment.