services.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. local M = {}
  2. local logger = require("core.log"):get_default()
  3. local function find_root_dir()
  4. if lvim.builtin.rooter.active then
  5. --- use vim-rooter to set root_dir
  6. vim.cmd "let root_dir = FindRootDirectory()"
  7. return vim.api.nvim_get_var "root_dir"
  8. end
  9. -- TODO: Rework this to not make it javascript specific
  10. --- use LSP to set root_dir
  11. local lsp_utils = require "lsp.utils"
  12. local ts_client = lsp_utils.get_active_client_by_ft "typescript"
  13. if ts_client == nil then
  14. logger.error "Unable to determine root directory since tsserver didn't start correctly"
  15. return nil
  16. end
  17. return ts_client.config.root_dir
  18. end
  19. local function from_node_modules(command)
  20. local root_dir = find_root_dir()
  21. if not root_dir then
  22. return nil
  23. end
  24. return root_dir .. "/node_modules/.bin/" .. command
  25. end
  26. local local_providers = {
  27. prettier = { find = from_node_modules },
  28. prettierd = { find = from_node_modules },
  29. prettier_d_slim = { find = from_node_modules },
  30. eslint_d = { find = from_node_modules },
  31. eslint = { find = from_node_modules },
  32. }
  33. function M.find_command(command)
  34. if local_providers[command] then
  35. local local_command = local_providers[command].find(command)
  36. if local_command and vim.fn.executable(local_command) == 1 then
  37. return local_command
  38. end
  39. end
  40. if vim.fn.executable(command) == 1 then
  41. return command
  42. end
  43. return nil
  44. end
  45. return M