aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/npm.lua
blob: b165d5f1e7eb6461d4e03e7b77643dcad7b1e331 (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
79
80
81
82
83
84
85
local path = require "nvim-lsp-installer.path"
local fs = require "nvim-lsp-installer.fs"
local Data = require "nvim-lsp-installer.data"
local installers = require "nvim-lsp-installer.installers"
local std = require "nvim-lsp-installer.installers.std"
local platform = require "nvim-lsp-installer.platform"
local process = require "nvim-lsp-installer.process"

local M = {}

local npm = platform.is_win and "npm.cmd" or "npm"

local function ensure_npm(installer)
    return installers.pipe {
        std.ensure_executables {
            { "node", "node was not found in path. Refer to https://nodejs.org/en/." },
            {
                "npm",
                "npm was not found in path. Refer to https://docs.npmjs.com/downloading-and-installing-node-js-and-npm.",
            },
        },
        installer,
    }
end

local function create_installer(read_version_from_context)
    return function(packages)
        return ensure_npm(function(server, callback, context)
            local pkgs = Data.list_copy(packages or {})
            local c = process.chain {
                cwd = server.root_dir,
                stdio_sink = context.stdio_sink,
            }
            -- stylua: ignore start
            if not (fs.dir_exists(path.concat { server.root_dir, "node_modules" }) or
                   fs.file_exists(path.concat { server.root_dir, "package.json" }))
            then
                c.run(npm, { "init", "--yes", "--scope=lsp-installer" })
            end

            if read_version_from_context and context.requested_server_version and #pkgs > 0 then
                -- The "head" package is the recipient for the requested version. It's.. by design... don't ask.
                pkgs[1] = ("%s@%s"):format(pkgs[1], context.requested_server_version)
            end

            -- stylua: ignore end
            c.run(npm, vim.list_extend({ "install" }, pkgs))
            c.spawn(callback)
        end)
    end
end

M.packages = create_installer(true)
M.install = create_installer(false)

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

function M.run(script)
    return ensure_npm(function(server, callback, context)
        process.spawn(npm, {
            args = { "run", script },
            cwd = server.root_dir,
            stdio_sink = context.stdio_sink,
        }, callback)
    end)
end

function M.executable(root_dir, executable)
    return path.concat {
        root_dir,
        "node_modules",
        ".bin",
        platform.is_win and ("%s.cmd"):format(executable) or executable,
    }
end

return M