aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/zx.lua
blob: 7bebdc332bfbb50c7b29ce9f816d2adccd89f029 (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
68
69
70
71
72
73
74
75
76
77
78
local fs = require "nvim-lsp-installer.fs"
local path = require "nvim-lsp-installer.path"
local installers = require "nvim-lsp-installer.installers"
local platform = require "nvim-lsp-installer.platform"
local npm = require "nvim-lsp-installer.installers.npm"
local process = require "nvim-lsp-installer.process"

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, opts)
        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
            opts.stdio_sink.stdout(("Preparing for installation… (npm %s zx)"):format(npm_command))
        end

        fs.mkdirp(INSTALL_DIR)

        -- todo use process installer
        local handle, pid = process.spawn(platform.is_win and "npm.cmd" or "npm", {
            args = { npm_command, "zx@1" },
            cwd = INSTALL_DIR,
            stdio_sink = opts.stdio_sink,
        }, function(success)
            if success then
                has_installed_zx = true
                callback(true)
            else
                opts.stdio_sink.stderr "Failed to install zx."
                callback(false)
            end
        end)

        if handle == nil then
            opts.stdio_sink.stderr(("Failed to install/update zx. %s"):format(pid))
            callback(false)
        end
    end
end

local function exec(file)
    return function(server, callback, context)
        process.spawn(ZX_EXECUTABLE, {
            args = { file },
            cwd = server.root_dir,
            stdio_sink = context.stdio_sink,
        }, callback)
    end
end

function M.file(relpath)
    local script_path = path.realpath(relpath, 3)
    return installers.compose {
        exec(("file:///%s"):format(script_path)),
        zx_installer(false),
    }
end

return M