services.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. local M = {}
  2. local function find_root_dir()
  3. local util = require "lspconfig/util"
  4. local lsp_utils = require "lsp.utils"
  5. local status_ok, ts_client = lsp_utils.is_client_active "typescript"
  6. if status_ok then
  7. return ts_client.config.root_dir
  8. end
  9. local dirname = vim.fn.expand "%:p:h"
  10. return util.root_pattern "package.json"(dirname)
  11. end
  12. local function from_node_modules(command)
  13. local logger = require("core.log"):get_default()
  14. local root_dir = find_root_dir()
  15. if not root_dir then
  16. logger.error(string.format("Unable to find the [%s] node module.", command))
  17. return nil
  18. end
  19. return root_dir .. "/node_modules/.bin/" .. command
  20. end
  21. local local_providers = {
  22. prettier = { find = from_node_modules },
  23. prettierd = { find = from_node_modules },
  24. prettier_d_slim = { find = from_node_modules },
  25. eslint_d = { find = from_node_modules },
  26. eslint = { find = from_node_modules },
  27. }
  28. function M.find_command(command)
  29. if local_providers[command] then
  30. local local_command = local_providers[command].find(command)
  31. if local_command and vim.fn.executable(local_command) == 1 then
  32. return local_command
  33. end
  34. end
  35. if vim.fn.executable(command) == 1 then
  36. return command
  37. end
  38. return nil
  39. end
  40. return M