|
| 1 | +local cache = require("prettier.cache") |
1 | 2 | local find_git_ancestor = require("lspconfig.util").find_git_ancestor
|
2 | 3 | local find_package_json_ancestor = require("lspconfig.util").find_package_json_ancestor
|
3 | 4 | local path_join = require("lspconfig.util").path.join
|
@@ -26,23 +27,73 @@ function M.prettier_enabled()
|
26 | 27 | return config_file_exists()
|
27 | 28 | end
|
28 | 29 |
|
29 |
| ----@param cmd string |
30 |
| ----@return nil|string |
31 |
| -function M.resolve_bin(cmd) |
32 |
| - local project_root = get_working_directory() |
| 30 | +---@param _cwd string |
| 31 | +---@param scope '"global"'|'"local"' |
| 32 | +---@return string|false bin_dir |
| 33 | +local function _get_bin_dir(_cwd, scope) |
| 34 | + local cmd = "npm bin" |
| 35 | + if scope == "global" then |
| 36 | + cmd = cmd .. " --global" |
| 37 | + end |
33 | 38 |
|
34 |
| - if project_root then |
35 |
| - local local_bin = path_join(project_root, "/node_modules/.bin", cmd) |
36 |
| - if vim.fn.executable(local_bin) == 1 then |
37 |
| - return local_bin |
38 |
| - end |
| 39 | + local result = vim.fn.systemlist(cmd) |
| 40 | + if vim.fn.isdirectory(result[1]) == 1 then |
| 41 | + return result[1] |
| 42 | + end |
| 43 | + |
| 44 | + return false |
| 45 | +end |
| 46 | + |
| 47 | +---@type fun(cwd: string, scope: '"global"'|'"local"'): string|false |
| 48 | +local get_bin_dir = cache.wrap(_get_bin_dir, function(cwd, scope) |
| 49 | + return scope .. "::" .. cwd |
| 50 | +end) |
| 51 | + |
| 52 | +---@param name string |
| 53 | +---@param scope '"global"'|'"local"' |
| 54 | +---@return string|false bin |
| 55 | +local function _get_bin_path(cwd, name, scope) |
| 56 | + local bin_dir = get_bin_dir(cwd, scope) |
| 57 | + if not bin_dir then |
| 58 | + return false |
| 59 | + end |
| 60 | + |
| 61 | + local bin = path_join(bin_dir, name) |
| 62 | + if vim.fn.executable(bin) == 1 then |
| 63 | + return bin |
39 | 64 | end
|
40 | 65 |
|
41 |
| - if vim.fn.executable(cmd) == 1 then |
42 |
| - return cmd |
| 66 | + if scope == "global" and vim.fn.executable(name) == 1 then |
| 67 | + return vim.fn.exepath(name) |
| 68 | + end |
| 69 | + |
| 70 | + return false |
| 71 | +end |
| 72 | + |
| 73 | +---@type fun(cwd: string, name: string, scope: '"global"'|'"local"'): string|false |
| 74 | +local get_bin_path = cache.wrap(_get_bin_path, function(cwd, name, scope) |
| 75 | + return scope .. "::" .. name .. "::" .. cwd |
| 76 | +end) |
| 77 | + |
| 78 | +---@param name string |
| 79 | +---@param preference? '"global"'|'"local"'|'"prefer-local"' |
| 80 | +---@return string|false |
| 81 | +function M.resolve_bin(name, preference) |
| 82 | + local cwd = vim.fn.getcwd() |
| 83 | + |
| 84 | + preference = preference or "prefer-local" |
| 85 | + |
| 86 | + if preference == "global" then |
| 87 | + return get_bin_path(cwd, name, "global") |
| 88 | + end |
| 89 | + |
| 90 | + local bin = get_bin_path(cwd, name, "local") |
| 91 | + |
| 92 | + if bin or preference == "local" then |
| 93 | + return bin |
43 | 94 | end
|
44 | 95 |
|
45 |
| - return nil |
| 96 | + return get_bin_path(cwd, name, "global") |
46 | 97 | end
|
47 | 98 |
|
48 | 99 | function M.tbl_flatten(tbl, should_flatten, result, prefix, depth)
|
|
0 commit comments