Skip to content

only kill browsers if any yet running started from Vim #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions plugin/vim-live-server.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@
" By Wolandark
" https://github.com/wolandark/vim-live-server

if executable('browser-sync')
let s:browsersync_counter = 0
"Browser-Sync
function! StartBrowserSync()
" let cmd = "browser-sync start --no-notify --server --cwd=" . getcwd() . " --files \"*.html, *.css, *.js\" &"
let cmd = "browser-sync start --no-notify --server --files *.html, *.css, *.js &"
call system(cmd)
echo "BrowserSync started in the background."
let s:browsersync_counter += 1
endfunction

function! StartBrowserSyncOnPort(port)
let port_num = a:port + 0 " Convert a:port to a number
let cmd = "browser-sync start --no-notify --server --cwd=" . getcwd() . " --port=" . port_num . " --files \"*.html, *.css, *.js\" &"
call system(cmd)
echo "BrowserSync started in the background on port " . port_num . "."
let s:browsersync_counter += 1
endfunction

command! StartBrowserSync call StartBrowserSync()
command! -nargs=1 StartBrowserSyncOnPort call StartBrowserSyncOnPort(<f-args>)

if executable('pkill')
function! KillBrowserSync()
let port = systemlist("pgrep -f 'browser-sync'")[0]
if empty(port)
Expand All @@ -28,38 +36,56 @@ function! KillBrowserSync()
call system(cmd)
echo "BrowserSync server on port 3000 terminated."
endif
if s:browsersync_counter > 0 | let s:browsersync_counter -= 1 | endif
endfunction

function! KillBrowserSyncOnPort(port)
let cmd = "pgrep -f 'browser-sync.*--port=" . a:port . "' | xargs -r kill"
call system(cmd)
echo "BrowserSync server on port " . a:port . " terminated."
if s:browsersync_counter > 0 | let s:browsersync_counter -= 1 | endif
endfunction

function! KillAllBrowserSyncInstances()
let cmd = "pkill -f 'browser-sync'"
call system(cmd)
let s:browsersync_counter = 0
endfunction

command! KillBrowserSync call KillBrowserSync()
command! -nargs=1 KillBrowserSyncOnPort call KillBrowserSyncOnPort(<f-args>)

augroup BrowserSyncKill
autocmd!
autocmd VimLeave * call KillAllBrowserSyncInstances()
autocmd VimLeave * if s:browsersync_counter > 0 | call KillAllBrowserSyncInstances() | endif
augroup END
endif
endif

" Live-Server
if executable('live-server')
let s:liveserver_counter = 0

function! StartLiveServer()
let cmd = "live-server &"
call system(cmd)
echo "Live server started in the background."
let s:liveserver_counter += 1
endfunction

function! StartLiveServerOnPort(port)
let port_num = a:port + 0 " Convert a:port to a number
let cmd = "live-server --port=" . port_num . "&"
call system(cmd)
echo "Live Server started in the background on port " . port_num . "."
let s:liveserver_counter += 1
endfunction

" Call Commands
command! StartLiveServer call StartLiveServer()
command! -nargs=1 StartLiveServerOnPort call StartLiveServerOnPort(<f-args>)

if executable('pkill')
function! KillLiveServer()
let port = systemlist("pgrep -f 'live-server'")[0]
if empty(port)
Expand All @@ -69,30 +95,28 @@ function! KillLiveServer()
call system(cmd)
echo "Live Server on port 8080 terminated."
endif
if s:liveserver_counter > 0 | let s:liveserver_counter -= 1 | endif
endfunction

function! KillLiveServerOnPort(port)
let cmd = "pgrep -f 'live-server.*--port=" . a:port . "' | xargs -r kill"
call system(cmd)
echo "Live Server on port " . a:port . " terminated."
if s:liveserver_counter > 0 | let s:liveserver_counter -= 1 | endif
endfunction

function! KillAllLiveServerInstances()
let cmd = "pkill -f 'live-server'"
call system(cmd)
let s:liveserver_counter = 0
endfunction

command! KillLiveServer call KillLiveServer()
command! -nargs=1 KillLiveServerOnPort call KillLiveServerOnPort(<f-args>)

augroup LiveServerKill
autocmd!
autocmd VimLeave * call KillAllLiveServerInstances()
autocmd VimLeave * if s:liveserver_counter > 0 | call KillAllLiveServerInstances() | endif
augroup END

" Call Commands
command! StartBrowserSync call StartBrowserSync()
command! StartLiveServer call StartLiveServer()
command! -nargs=1 StartBrowserSyncOnPort call StartBrowserSyncOnPort(<f-args>)
command! -nargs=1 StartLiveServerOnPort call StartLiveServerOnPort(<f-args>)
command! KillBrowserSync call KillBrowserSync()
command! KillLiveServer call KillLiveServer()
command! -nargs=1 KillBrowserSyncOnPort call KillBrowserSyncOnPort(<f-args>)
command! -nargs=1 KillLiveServerOnPort call KillLiveServerOnPort(<f-args>)
endif
endif