-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(astro): use vendored tsdk if workspace tsdk not available (#75)
Closes #74.
- Loading branch information
1 parent
0eb7cfe
commit e492f12
Showing
3 changed files
with
54 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |