aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2021-05-17 16:48:06 +0200
committerGitHub <noreply@github.com>2021-05-17 16:48:06 +0200
commit6a3a3fee295c6811ec0a420d3bd4f92e973baa29 (patch)
treedf8580f45a7518ed5a394002f91073fd27ab6fb2 /lua
parents/install_cmd/installer/g (#15) (diff)
downloadmason-6a3a3fee295c6811ec0a420d3bd4f92e973baa29.tar
mason-6a3a3fee295c6811ec0a420d3bd4f92e973baa29.tar.gz
mason-6a3a3fee295c6811ec0a420d3bd4f92e973baa29.tar.bz2
mason-6a3a3fee295c6811ec0a420d3bd4f92e973baa29.tar.lz
mason-6a3a3fee295c6811ec0a420d3bd4f92e973baa29.tar.xz
mason-6a3a3fee295c6811ec0a420d3bd4f92e973baa29.tar.zst
mason-6a3a3fee295c6811ec0a420d3bd4f92e973baa29.zip
zx: compose installer with shell.raw (#16)
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-lsp-installer/installers/init.lua28
-rw-r--r--lua/nvim-lsp-installer/installers/zx.lua80
2 files changed, 66 insertions, 42 deletions
diff --git a/lua/nvim-lsp-installer/installers/init.lua b/lua/nvim-lsp-installer/installers/init.lua
new file mode 100644
index 00000000..8cc0e34c
--- /dev/null
+++ b/lua/nvim-lsp-installer/installers/init.lua
@@ -0,0 +1,28 @@
+local M = {}
+
+function M.compose(installers)
+ if #installers == 0 then
+ error("No installers to compose.")
+ end
+
+ return function (server, callback)
+ local function execute(idx)
+ installers[idx](server, function (success, result)
+ if not success then
+ -- oh no, error. exit early
+ callback(success, result)
+ elseif installers[idx - 1] then
+ -- iterate
+ execute(idx - 1)
+ else
+ -- we done
+ callback(success, result)
+ end
+ end)
+ end
+
+ execute(#installers)
+ end
+end
+
+return M
diff --git a/lua/nvim-lsp-installer/installers/zx.lua b/lua/nvim-lsp-installer/installers/zx.lua
index 92027ef8..6d628e19 100644
--- a/lua/nvim-lsp-installer/installers/zx.lua
+++ b/lua/nvim-lsp-installer/installers/zx.lua
@@ -1,5 +1,7 @@
local fs = require("nvim-lsp-installer.fs")
local path = require("nvim-lsp-installer.path")
+local installers = require("nvim-lsp-installer.installers")
+local shell = require("nvim-lsp-installer.installers.shell")
local uv = vim.loop
@@ -10,59 +12,53 @@ local ZX_EXECUTABLE = path.concat { INSTALL_DIR, "node_modules", ".bin", "zx" }
local has_installed_zx = false
-function M.install_zx(callback, force)
+local function zx_installer(force)
force = force or false -- be careful with boolean logic if flipping this
- if has_installed_zx and not force then
- callback()
- return
- end
+ 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
- error("Cannot install zx because npm and/or node not installed.")
- 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.")
+ end
- local is_zx_already_installed = fs.file_exists(ZX_EXECUTABLE)
- local npm_command = is_zx_already_installed and "update" or "install"
+ 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))
+ print(("Preparing for :LspInstall, please wait… ($ npm %s zx)"):format(npm_command))
- fs.mkdirp(INSTALL_DIR)
+ 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))
+ uv.spawn(
+ "npm",
+ {
+ args = { npm_command, "zx" },
+ cwd = INSTALL_DIR,
+ },
+ vim.schedule_wrap(function (code)
+ if code ~= 0 then
+ callback(false, "Failed to install zx.")
+ 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 function (server, callback)
- 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 = function (_, exit_code)
- if exit_code ~= 0 then
- callback(false, ("Exit code was non-successful: %d"):format(exit_code))
- else
- callback(true, nil)
- end
- end
- })
- vim.cmd [[startinsert]] -- so that the buffer tails the term log nicely
- end, false)
- end
+ return installers.compose {
+ shell.raw(("set -e; %q %q"):format(
+ ZX_EXECUTABLE,
+ script_path
+ )),
+ zx_installer(false)
+ }
end
return M