From 894692f380b3c952f94ef0097960a5c53b4dc8be Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Jan 2023 20:39:42 -0600 Subject: [PATCH] initial commit --- .gitignore | 3 ++ .stylua.toml | 4 +++ doc/live-server.txt | 44 ++++++++++++++++++++++++ lua/live-server.lua | 82 +++++++++++++++++++++++++++++++++++++++++++++ readme.md | 48 ++++++++++++++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 .gitignore create mode 100644 .stylua.toml create mode 100644 doc/live-server.txt create mode 100644 lua/live-server.lua create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43abc87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +doc/tags +vim.toml +selene.toml diff --git a/.stylua.toml b/.stylua.toml new file mode 100644 index 0000000..54e6142 --- /dev/null +++ b/.stylua.toml @@ -0,0 +1,4 @@ +quote_style = "AutoPreferSingle" +call_parentheses = "None" +indent_type = "Spaces" +column_width = 80 diff --git a/doc/live-server.txt b/doc/live-server.txt new file mode 100644 index 0000000..c60721a --- /dev/null +++ b/doc/live-server.txt @@ -0,0 +1,44 @@ +*live-server* *live-server.txt* + +Author: Barrett Ruth +Homepage: + +=============================================================================== +INTRODUCTION *live-server.nvim* + +live-server.nvim automatically reloads HTML, CSS, and JavaScript files in the +browser via a local development server. + +Author: Barrett Ruth + +=============================================================================== +SETUP *live-server.setup()* +>lua + require('live-server').setup(config) +< +Parameters: ~ + + {config} `(table | nil)`: (Optional) table containing configuration for + live-server. Defaults shown below. + +Usage: ~ +>lua + require('live-server').setup { + -- Arguments passed to live-server via `vim.fn.jobstart()` + -- Run `live-server --help` to see list of available options + -- For example, to use port 7000 and browser firefox: + -- args = { '--port=7000', '--browser=firefox' } + args = {} + } +< +=============================================================================== +COMMANDS *live-server.commands* + + *LiveServerStart* +:LiveServerStart Start the live server. + + *LiveServerStop* +:LiveServerStop Stop the live server. + +------------------------------------------------------------------------------- +vim:tw=80:ft=help: diff --git a/lua/live-server.lua b/lua/live-server.lua new file mode 100644 index 0000000..a8635e3 --- /dev/null +++ b/lua/live-server.lua @@ -0,0 +1,82 @@ +local M = {} + +local function log(message, level) + vim.notify_once( + string.format('import-cost.nvim: %s', message), + vim.log.levels[level] + ) +end + +M.config = { + -- let live-server handle the defaults + args = {}, +} + +M.setup = function(user_config) + M.config = vim.tbl_deep_extend('force', M.config, user_config or {}) + + if not vim.fn.executable 'live-server' then + log( + 'live-server is not executable. Ensure the npm module is properly installed', + vim.log.levels.ERROR + ) + return + end + + vim.api.nvim_create_user_command('LiveServerStart', M.start, {}) + vim.api.nvim_create_user_command('LiveServerStop', M.stop, {}) +end + +local job_cache = {} + +M.start = function() + local bufnr = vim.api.nvim_get_current_buf() + + local cmd = { 'live-server' } + vim.list_extend(cmd, M.config.args) + + if job_cache[bufnr] then + log('live-server instance already running', 'INFO') + return + end + + local job_id = vim.fn.jobstart(cmd, { + on_stderr = function(_, data) + if data[1] == '' then + return + end + + log(data[1]:match '.-m(.-)\27', 'ERROR') + end, + on_exit = function(_, exit_code) + job_cache[bufnr] = nil + + if exit_code == 0 then + return + end + + log( + string.format( + 'live-server stopped unexpectedly with exit code %s', + exit_code + ), + 'ERROR' + ) + end, + }) + + job_cache[bufnr] = job_id +end + +M.stop = function() + local bufnr = vim.api.nvim_get_current_buf() + + if job_cache[bufnr] then + vim.fn.jobstop(job_cache[bufnr]) + job_cache[bufnr] = nil + else + log('no live-server instance running', 'INFO') + end +end + +return M diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4428c9f --- /dev/null +++ b/readme.md @@ -0,0 +1,48 @@ +# live-server.nvim + +Live reload HTML, CSS, and JavaScript files inside neovim with the power of +[live-server](https://www.npmjs.com/package/live-server). + +## Installation + +1. Install [live-server](https://www.npmjs.com/package/live-server) globally + with your node.js package manager of choice. + +2. Install regularly with your neovim package manager + +For example, a config with [yarn](https://yarnpkg.com) and +[lazy.nvim](https://github.com/folke/lazy.nvim) may look like the following: + +```lua +require('lazy').setup { + { + 'barrett-ruth/live-server.nvim', + build = 'yarn global add live-server' + config = true + } +} +``` + +## Configuration + +Configure via the setup function (or use the defaults with no arguments): + +```lua +require('live-server').setup(opts) +``` + +See `:h live-server` for more information + +## Usage + +`:LiveServerStart`: start the live server + +`:LiveServerStop`: stop the live-server + +## Acknowledgements + +1. [mazeloth/live-server](https://github.com/manzeloth/live-server/blob/main/plugin/live-server.vim): + the first ever live-server port to vim +2. [aurum77/live-server.nvim](https://github.com/aurum77/live-server.nvim): the + first live-server port to neovim (however, it unecessarily depends on `npm` + and didn't quite cut it for me)