aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/zx.lua
blob: 5308ac66b3407e68a5e827b162a9f5801d292a85 (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
63
64
65
66
67
local fs = require "nvim-lsp-installer.fs"
local path = require "nvim-lsp-installer.path"
local notify = require "nvim-lsp-installer.notify"
local installers = require "nvim-lsp-installer.installers"
local shell = require "nvim-lsp-installer.installers.shell"
local npm = require "nvim-lsp-installer.installers.npm"

local uv = vim.loop

local M = {}

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

local has_installed_zx = false

local function zx_installer(force)
    force = force or false -- be careful with boolean logic if flipping this

    return function(_, callback)
        if has_installed_zx and not force then
            callback(true, "zx already installed")
            return
        end

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

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

        if not is_zx_already_installed then
            notify(("Preparing for :LspInstall… ($ npm %s zx)"):format(npm_command))
        end

        fs.mkdirp(INSTALL_DIR)

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

function M.file(relpath)
    local script_path = path.realpath(relpath, 3)
    return installers.compose {
        shell.raw(("%q %q"):format(ZX_EXECUTABLE, script_path)),
        zx_installer(false),
    }
end

return M