live-server: avoid duplicate instances
This commit is contained in:
parent
46df6ae7e6
commit
61f18ef900
1 changed files with 25 additions and 9 deletions
|
|
@ -7,6 +7,22 @@ local function log(message, level)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local job_cache = {}
|
||||||
|
|
||||||
|
local function find_cached_dir(dir)
|
||||||
|
local cur = dir
|
||||||
|
|
||||||
|
while not job_cache[cur] do
|
||||||
|
if cur == '/' then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
cur = vim.fn.fnamemodify(cur, ':h')
|
||||||
|
end
|
||||||
|
|
||||||
|
return cur
|
||||||
|
end
|
||||||
|
|
||||||
M.config = {
|
M.config = {
|
||||||
-- let live-server handle the defaults
|
-- let live-server handle the defaults
|
||||||
args = {},
|
args = {},
|
||||||
|
|
@ -27,19 +43,18 @@ M.setup = function(user_config)
|
||||||
vim.api.nvim_create_user_command('LiveServerStop', M.stop, {})
|
vim.api.nvim_create_user_command('LiveServerStop', M.stop, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
local job_cache = {}
|
|
||||||
|
|
||||||
M.start = function()
|
M.start = function()
|
||||||
local dir = vim.fn.expand '%:p:h'
|
local dir = vim.fn.expand '%:p:h'
|
||||||
|
local cached_dir = find_cached_dir(dir) or dir
|
||||||
|
|
||||||
local cmd = { 'live-server' }
|
if cached_dir then
|
||||||
vim.list_extend(cmd, M.config.args)
|
|
||||||
|
|
||||||
if job_cache[dir] then
|
|
||||||
log('live-server instance already running', 'INFO')
|
log('live-server instance already running', 'INFO')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local cmd = { 'live-server', cached_dir }
|
||||||
|
vim.list_extend(cmd, M.config.args)
|
||||||
|
|
||||||
local job_id = vim.fn.jobstart(cmd, {
|
local job_id = vim.fn.jobstart(cmd, {
|
||||||
on_stderr = function(_, data)
|
on_stderr = function(_, data)
|
||||||
if not data or data[1] == '' then
|
if not data or data[1] == '' then
|
||||||
|
|
@ -66,10 +81,11 @@ end
|
||||||
|
|
||||||
M.stop = function()
|
M.stop = function()
|
||||||
local dir = vim.fn.expand '%:p:h'
|
local dir = vim.fn.expand '%:p:h'
|
||||||
|
local cached_dir = find_cached_dir(dir)
|
||||||
|
|
||||||
if job_cache[dir] then
|
if cached_dir then
|
||||||
vim.fn.jobstop(job_cache[dir])
|
vim.fn.jobstop(job_cache[cached_dir])
|
||||||
job_cache[dir] = nil
|
job_cache[cached_dir] = nil
|
||||||
else
|
else
|
||||||
log('no live-server instance running', 'INFO')
|
log('no live-server instance running', 'INFO')
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue