diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2023-03-28 23:46:07 +0200 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2023-03-28 23:46:07 +0200 |
commit | 350a728d1127bea8e51b4e9f00d3c9f078cfd3cc (patch) | |
tree | d09fb43f19250ac492fc5fdb7155c376db0e799d /config/essentials/nvim/lua/user/live-server.lua | |
parent | 299f1e8a61d0a36b697781cf8ff5f84bac07f049 (diff) |
use selfmade live-server plugin
Diffstat (limited to 'config/essentials/nvim/lua/user/live-server.lua')
-rw-r--r-- | config/essentials/nvim/lua/user/live-server.lua | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/config/essentials/nvim/lua/user/live-server.lua b/config/essentials/nvim/lua/user/live-server.lua new file mode 100644 index 0000000..c98729c --- /dev/null +++ b/config/essentials/nvim/lua/user/live-server.lua @@ -0,0 +1,53 @@ +local M = {} +-- keep track of jobs +local live_servers = {} + +function M.start_live_server() + -- Search for available port and use it + local port = 5500 + local running = true + while running do + local output = vim.fn.systemlist('lsof -i :' .. port) + if #output == 0 then + running = false + else + port = port + 1 + end + end + + local command = "live-server --no-browser --port=" .. port .. " " .. vim.fn.expand("%:p:h") + -- run + local job_id = vim.fn.jobstart(command, { + on_exit = function(_, _, _) end + }) + -- save + live_servers[port] = job_id + + print("Started live-server on :" .. port .. ".") +end + +function M.stop_live_servers() + for port, job_id in pairs(live_servers) do + local output = vim.fn.systemlist('lsof -i :' .. port) + if #output > 0 then + vim.fn.jobstop(job_id) + print("Killed live-server on :" .. port .. ".") + end + live_servers[port] = nil + end +end + +vim.api.nvim_create_user_command("LiveServer", function(opts) + local opt = string.format(opts.args) + if #opts.args == 0 then + M.start_live_server() + elseif opt == "start" then + M.start_live_server() + elseif opt == "stop" then + M.stop_live_servers() + else + print("Invalid argument. Usage: LiveServer [start|stop]") + end +end, { nargs = '*' }) + +return M |