aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/zx.lua
blob: 20ee9bb1e35ac95fda2ea82a46a6e9362fa61d04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local fs = require("nvim-lsp-installer.fs")
local path = require("nvim-lsp-installer.path")

local uv = vim.loop

local M = {}

local INSTALL_DIR = path.concat { vim.fn.stdpath("data"), "lsp_servers", ".zx" }
local ZX_EXECUTABLE = path.concat { INSTALL_DIR, "node_modules", ".bin", "zx" }

local has_installed_zx = false

function M.install_zx(callback, force)
    force = force or false -- be careful with boolean logic if flipping this

    if has_installed_zx and not force then
        callback()
        return
    end

    if vim.fn.executable("npm") ~= 1 or vim.fn.executable("node") ~= 1 then
        error("Cannot install zx because npm and/or node not installed.")
    end

    local is_zx_already_installed = fs.file_exists(ZX_EXECUTABLE)
    local npm_command = is_zx_already_installed and "update" or "install"

    print(("Preparing for :LspInstall, please wait… ($ npm %s zx)"):format(npm_command))

    fs.mkdirp(INSTALL_DIR)

    uv.spawn("npm", {
        args = { npm_command, "zx" },
        cwd = INSTALL_DIR,
    }, vim.schedule_wrap(function (code)
        if code ~= 0 then
            error("Failed to install zx.")
        end
        has_installed_zx = true
        vim.cmd [[ echon "" ]] -- clear the previously printed feedback message… ¯\_(ツ)_/¯
        callback()
    end))
end

function M.file(relpath)
    local script_path = path.realpath(relpath, 3)
    return function (server, on_exit)
        M.install_zx(function ()
            vim.cmd [[new]]
            vim.fn.termopen(("set -e; %q %q"):format(
                ZX_EXECUTABLE,
                script_path
            ), {
                    cwd = server._root_dir,
                    on_exit = on_exit
                })
            vim.cmd [[startinsert]] -- so that the buffer tails the term log nicely
        end, false)
    end
end

return M