Skip to content

Commit

Permalink
Merge pull request #1 from Diogo-ss/dev
Browse files Browse the repository at this point in the history
 improvements
  • Loading branch information
Diogo-ss authored Mar 26, 2024
2 parents bce429e + 0303a8b commit 8396adc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 39 deletions.
17 changes: 7 additions & 10 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Integrates Five-Server and Neovim. View dynamic and static pages.

## ✨ Features:

- Debugging by notifications (optional)
- Bebug via notify (optional)
- Log file (optional)
- Supports fiveserverrc
- automatic installation with NPM or PNPM
- no global installation required
- multiple instances in a single neovim session
- Automatic installation with NPM or PNPM
- No global installation required
- Multiple instances in a single Neovim session

## 🎯 Requirements

Expand Down Expand Up @@ -45,7 +45,7 @@ These are the standard options. Change them according to your needs.
-- configure debug.
debug = {
-- enables or disables debug.
enabled = true,
enabled = false,
-- log filename.
file_name = "fs-debug.log",
},
Expand All @@ -67,12 +67,11 @@ These are the standard options. Change them according to your needs.
-- RC filename (not recommended to change).
path = ".fiveserverrc",
},
-- settings to be saved in Five Server RC.
-- settings to be saved in Five-Server RC.
-- the key name and value will be used.
config = {
-- set the server port.
port = 5500,

-- the output level.
logLevel = 3,

Expand Down Expand Up @@ -104,6 +103,4 @@ All commands use auto complete, allowing you to use flags to change their behavi
`:FiveServer stop <id>`: enter the ID of the instance to be stopped (all valid IDs will be provided). </br>
`:FiveServer gen_rc`: generates a config file based on the user configuration. </br>
`:FiveServer gen_rc --force`: generates a config file based on the user options. If a config already exists, it will be overwritten. </br>



`:FiveServer install`: install the five-server client using NPM or PNPM.
6 changes: 3 additions & 3 deletions lua/fs/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ M.opts = {
-- configure debug.
debug = {
-- enables or disables debug.
enabled = true,
enabled = false,
-- log filename.
file_name = "fs-debug.log",
},
-- Five Server bin directory.
-- if installed globally, use: `bin = "five-server"`
bin = vim.fn.stdpath "data" .. "/five-server/node_modules/.bin/five-server",
bin = vim.fn.stdpath "data" .. "/fs-plugin/five-server/node_modules/.bin/five-server",
-- Directory for installing Five Server
path = vim.fn.stdpath "data" .. "/five-server",
path = vim.fn.stdpath "data" .. "/fs-plugin/five-server",
-- notifications on the interface.
notify = true,
-- configure Five Server RC.
Expand Down
6 changes: 3 additions & 3 deletions lua/fs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function M.setup(opts)

if has_valid_flag and #args >= 2 then
local ok, _sub_flags = pcall(commands.flags[args[2]].sub_flags)
if ok then
if ok and type(_sub_flags) == "table" then
sub_flags = _sub_flags
end
end
Expand All @@ -53,8 +53,8 @@ function M.setup(opts)
return commands.flag_filter(lead_arg, flags)
elseif #args == 2 and has_valid_flag and has_space then
return sub_flags
elseif #args == 3 and has_valid_flag and not has_space then
return commands.filter(lead_arg, sub_flags)
elseif #args == 3 and has_valid_flag and not has_space and not vim.tbl_isempty(sub_flags) then
return commands.flag_filter(lead_arg, sub_flags)
end
end,
})
Expand Down
33 changes: 29 additions & 4 deletions lua/fs/utils/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
--- flag to generate another file even if one already
--- exists in the current directory.
---
--- - `:FiveServer install`: Install Five-Server.
---
---NOTE: The commands will only be available after running setup().
---
---@brief ]]
Expand Down Expand Up @@ -51,6 +53,11 @@ M.flags = {
return
end

if flag then
logger.logger_warn("Invalid sub flag: " .. flag)
return
end

process.start()
end,
},
Expand All @@ -65,14 +72,20 @@ M.flags = {
---Stop Job with corresponding ID.
---@param id string: The ID of the process to stop.
execute = function(id)
id = tonumber(id) or process.get_path_instance(vim.fn.getcwd())
id = id or tostring(process.get_path_instance(vim.fn.getcwd()))

if not id:match "^%d+$" then
logger.logger_warn "Invalid ID format"
return
end

if not process.job_exists(id) then
logger.logger_warn "Job not found"
local num_id = tonumber(id)
if not num_id or not process.job_exists(num_id) then
logger.logger_warn "Invalid or no instance exists with ID"
return
end

process.stop(id)
process.stop(num_id)
end,
},

Expand All @@ -91,9 +104,21 @@ M.flags = {
return
end

if flag then
logger.logger_warn("Invalid sub flag: " .. flag)
return
end

rc.gen_rc()
end,
},

---Install flag.
install = {
execute = function(_)
require "fs.utils.install"()
end,
},
}

---Filters flags based on a leading argument.
Expand Down
26 changes: 12 additions & 14 deletions lua/fs/utils/install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ local config = require "fs.config"
local logger = require "fs.utils.logger"

local function cmd(command)
logger.logger_info("starting installation with: " .. tostring(command[1]))
logger.logger_info("Starting installation with: " .. tostring(command[1]))

vim.fn.jobstart(command, {
on_exit = function(_, code)
if code ~= 0 then
logger.logger_error("error when trying to install with: " .. table.concat(command, " "))
logger.logger_error("Error when trying to install with: " .. table.concat(command, " "))
else
logger.logger_info "Five Server was installed."
end
Expand All @@ -18,21 +18,19 @@ end
local function install(path)
path = path or config.opts.path

if vim.fn.isdirectory(path) ~= 1 then
vim.fn.mkdir(path, "p")
end
local package_managers = vim.tbl_filter(function(val)
return vim.fn.executable(val) == 1
end, { "npm", "pnpm" })

if vim.fn.executable "npm" == 1 then
cmd { "npm", "install", "five-server", "--prefix", path }
return
end
if #package_managers > 0 then
if vim.fn.isdirectory(path) ~= 1 then
vim.fn.mkdir(path, "p")
end

if vim.fn.executable "pnpm" == 1 then
cmd { "pnpm", "install", "five-server", "--prefix", path }
return
cmd { package_managers[1], "install", "five-server", "--prefix", path }
else
logger.logger_error "No package manager found (NPM ou PNPM)."
end

logger.logger_error "No package manager found (npm or pnpm)."
end

return install
6 changes: 1 addition & 5 deletions lua/fs/utils/rc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ function M.gen_rc(path, force)
path = path or config.opts.fiveserverrc.gen_rc.path

if M.has_rc(path) and not force then
local response = vim.fn.input "There is already a fiveserverrc. Would you like to generate another one? [y/N]: "
if response:lower() ~= "y" then
logger.logger_warn "Operation canceled."
return
end
return
end

M._gen_rc(path)
Expand Down

0 comments on commit 8396adc

Please sign in to comment.