refactor: use Util.read_file and Util.write_file for docs

This commit is contained in:
Folke Lemaitre 2022-12-15 14:08:04 +01:00
parent 70ca110ca1
commit 4dfab59c87
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 6 additions and 13 deletions

View File

@ -1,11 +1,6 @@
local M = {} local Util = require("lazy.util")
function M.read(file) local M = {}
local fd = assert(io.open(file, "r"))
local data = fd:read("*a") ---@type string
fd:close()
return data
end
function M.indent(str, indent) function M.indent(str, indent)
local lines = vim.split(str, "\n") local lines = vim.split(str, "\n")
@ -52,7 +47,7 @@ end
---@param contents table<string, string> ---@param contents table<string, string>
function M.save(contents) function M.save(contents)
local readme = M.read("README.md") local readme = Util.read_file("README.md")
contents.toc = M.toc(readme) contents.toc = M.toc(readme)
for tag, content in pairs(contents) do for tag, content in pairs(contents) do
content = M.fix_indent(content) content = M.fix_indent(content)
@ -69,14 +64,12 @@ function M.save(contents)
end end
end end
local fd = assert(io.open("README.md", "w+")) Util.write_file("README.md", readme)
fd:write(readme)
fd:close()
end end
---@return string ---@return string
function M.extract(file, pattern) function M.extract(file, pattern)
local init = M.read(file) local init = Util.read_file(file)
return assert(init:match(pattern)) return assert(init:match(pattern))
end end
@ -91,7 +84,7 @@ function M.update()
M.save({ M.save({
bootstrap = M.extract("lua/lazy/init.lua", "function M%.bootstrap%(%)\n(.-)\nend"), bootstrap = M.extract("lua/lazy/init.lua", "function M%.bootstrap%(%)\n(.-)\nend"),
config = config, config = config,
spec = M.read("lua/lazy/example.lua"), spec = Util.read_file("lua/lazy/example.lua"),
}) })
vim.cmd.checktime() vim.cmd.checktime()
end end