Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
refactor: make paths global
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Sep 15, 2023
1 parent 97f5896 commit f1a11a7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 50 deletions.
20 changes: 8 additions & 12 deletions lua/nvim-devdocs/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ local transpiler = require("nvim-devdocs.transpiler")

local function build_docs(entry, index, docs)
local alias = entry.slug:gsub("~", "-")
local current_doc_dir = config.new_path("docs", alias)

notify.log("Building " .. alias .. " documentation...")

local docs_dir = config.new_path("docs")
local current_doc_dir = config.new_path("docs", alias)
local index_path = config.new_path("index.json")
local lock_path = config.new_path("docs-lock.json")

if not docs_dir:exists() then docs_dir:mkdir() end
if not DOCS_DIR:exists() then DOCS_DIR:mkdir() end
if not INDEX_PATH:exists() then INDEX_PATH:write("{}", "w") end
if not LOCK_PATH:exists() then LOCK_PATH:write("{}", "w") end
if not current_doc_dir:exists() then current_doc_dir:mkdir() end
if not index_path:exists() then index_path:write("{}", "w") end
if not lock_path:exists() then lock_path:write("{}", "w") end

local section_map = {}
local path_map = {}
Expand Down Expand Up @@ -54,13 +50,13 @@ local function build_docs(entry, index, docs)
index.entries[i].path = path_map[index_entry.path] or path_map[main]
end

local index_parsed = vim.fn.json_decode(index_path:read())
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
index_parsed[alias] = index
index_path:write(vim.fn.json_encode(index_parsed), "w")
INDEX_PATH:write(vim.fn.json_encode(index_parsed), "w")

local lock_parsed = vim.fn.json_decode(lock_path:read())
local lock_parsed = vim.fn.json_decode(LOCK_PATH:read())
lock_parsed[alias] = entry
lock_path:write(vim.fn.json_encode(lock_parsed), "w")
LOCK_PATH:write(vim.fn.json_encode(lock_parsed), "w")

notify.log("Build complete!")
end
Expand Down
16 changes: 8 additions & 8 deletions lua/nvim-devdocs/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ local config = {

M.get = function() return config end

M.new_path = function(...) return path:new(config.dir_path, ...) end

M.setup = function(new_config)
if new_config ~= nil then
for key, value in pairs(new_config) do
config[key] = value
end
end

return config
end
DATA_DIR = M.new_path()
DOCS_DIR = M.new_path("docs")
INDEX_PATH = M.new_path("index.json")
LOCK_PATH = M.new_path("docs-lock.json")
REGISTERY_PATH = M.new_path("registery.json")

M.new_path = function(...)
if ... then
return path:new(config.dir_path, ...)
else
return path:new(config.dir_path)
end
return config
end

M.set_keymaps = function(bufnr, entry)
Expand Down
20 changes: 7 additions & 13 deletions lua/nvim-devdocs/list.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
local M = {}

local path = require("plenary.path")

local notify = require("nvim-devdocs.notify")
local plugin_config = require("nvim-devdocs.config").get()

local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
local registery_path = path:new(plugin_config.dir_path, "registery.json")

M.get_installed_alias = function()
if not lock_path:exists() then return {} end
if not LOCK_PATH:exists() then return {} end

local lockfile = lock_path:read()
local lockfile = LOCK_PATH:read()
local lock_parsed = vim.fn.json_decode(lockfile)
local installed = vim.tbl_keys(lock_parsed)

return installed
end

M.get_installed_entry = function()
if not registery_path:exists() then
if not REGISTERY_PATH:exists() then
notify.log_err("Devdocs registery not found, please run :DevdocsFetch")
return
end

local content = registery_path:read()
local content = REGISTERY_PATH:read()
local parsed = vim.fn.json_decode(content)
local installed = M.get_installed_alias()

Expand All @@ -39,12 +33,12 @@ M.get_installed_entry = function()
end

M.get_updatable = function()
if not registery_path:exists() or not lock_path:exists() then return {} end
if not REGISTERY_PATH:exists() or not LOCK_PATH:exists() then return {} end

local results = {}
local registery = registery_path:read()
local registery = REGISTERY_PATH:read()
local registery_parsed = vim.fn.json_decode(registery)
local lockfile = lock_path:read()
local lockfile = LOCK_PATH:read()
local lock_parsed = vim.fn.json_decode(lockfile)

for alias, value in pairs(lock_parsed) do
Expand Down
30 changes: 13 additions & 17 deletions lua/nvim-devdocs/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ local devdocs_site_url = "https://devdocs.io"
local devdocs_cdn_url = "https://documents.devdocs.io"

local plugin_config = config.get()
local data_dir = config.new_path()
local index_path = config.new_path("index.json")
local lock_path = config.new_path("docs-lock.json")
local registery_path = config.new_path("registery.json")

M.fetch = function()
notify.log("Fetching DevDocs registery...")
Expand All @@ -26,8 +22,8 @@ M.fetch = function()
["User-agent"] = "chrome", -- fake user agent, see #25
},
callback = function(response)
if not data_dir:exists() then data_dir:mkdir() end
registery_path:write(response.body, "w", 438)
if not DATA_DIR:exists() then DATA_DIR:mkdir() end
REGISTERY_PATH:write(response.body, "w", 438)
notify.log("DevDocs registery has been written to the disk")
end,
on_error = function(error)
Expand All @@ -37,7 +33,7 @@ M.fetch = function()
end

M.install = function(entry, verbose, is_update)
if not registery_path:exists() then
if not REGISTERY_PATH:exists() then
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
return
end
Expand Down Expand Up @@ -94,13 +90,13 @@ M.install = function(entry, verbose, is_update)
end

M.install_args = function(args, verbose, is_update)
if not registery_path:exists() then
if not REGISTERY_PATH:exists() then
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
return
end

local updatable = list.get_updatable()
local content = registery_path:read()
local content = REGISTERY_PATH:read()
local parsed = vim.fn.json_decode(content)

for _, arg in ipairs(args) do
Expand Down Expand Up @@ -132,15 +128,15 @@ M.uninstall = function(alias)
if not vim.tbl_contains(installed, alias) then
notify.log(alias .. " documentation is already uninstalled")
else
local index = vim.fn.json_decode(index_path:read())
local lockfile = vim.fn.json_decode(lock_path:read())
local index = vim.fn.json_decode(INDEX_PATH:read())
local lockfile = vim.fn.json_decode(LOCK_PATH:read())
local doc_path = config.new_path("docs", alias)

index[alias] = nil
lockfile[alias] = nil

index_path:write(vim.fn.json_encode(index), "w")
lock_path:write(vim.fn.json_encode(lockfile), "w")
INDEX_PATH:write(vim.fn.json_encode(index), "w")
LOCK_PATH:write(vim.fn.json_encode(lockfile), "w")
doc_path:rm({ recursive = true })

notify.log(alias .. " documentation has been uninstalled")
Expand All @@ -151,9 +147,9 @@ M.get_entries = function(alias)
local installed = list.get_installed_alias()
local is_installed = vim.tbl_contains(installed, alias)

if not index_path:exists() or not is_installed then return end
if not INDEX_PATH:exists() or not is_installed then return end

local index_parsed = vim.fn.json_decode(index_path:read())
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
local entries = index_parsed[alias].entries

for key, _ in ipairs(entries) do
Expand All @@ -164,10 +160,10 @@ M.get_entries = function(alias)
end

M.get_all_entries = function()
if not index_path:exists() then return {} end
if not INDEX_PATH:exists() then return {} end

local entries = {}
local index_parsed = vim.fn.json_decode(index_path:read())
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())

for alias, index in pairs(index_parsed) do
for _, doc_entry in ipairs(index.entries) do
Expand Down

0 comments on commit f1a11a7

Please sign in to comment.