feat(config): lazyvim.json is now versioned and migrates to a newer version when needed

This commit is contained in:
Folke Lemaitre
2023-10-12 21:45:32 +02:00
parent 8e71968c2b
commit c9892652d2
3 changed files with 34 additions and 14 deletions

View File

@ -128,6 +128,7 @@ local defaults = {
} }
M.json = { M.json = {
version = 1,
data = { data = {
version = nil, ---@type string? version = nil, ---@type string?
news = {}, ---@type table<string, string> news = {}, ---@type table<string, string>
@ -144,24 +145,13 @@ function M.json.load()
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true, array = true } }) local ok, json = pcall(vim.json.decode, data, { luanil = { object = true, array = true } })
if ok then if ok then
M.json.data = vim.tbl_deep_extend("force", M.json.data, json or {}) M.json.data = vim.tbl_deep_extend("force", M.json.data, json or {})
if M.json.data.hashes then if M.json.data.version ~= M.json.version then
---@diagnostic disable-next-line: no-unknown Util.json.migrate()
M.json.data.hashes = nil
M.json.save()
end end
end end
end end
end end
function M.json.save()
local path = vim.fn.stdpath("config") .. "/lazyvim.json"
local f = io.open(path, "w")
if f then
f:write(Util.json.encode(M.json.data))
f:close()
end
end
---@type LazyVimOptions ---@type LazyVimOptions
local options local options

View File

@ -1,3 +1,4 @@
local Config = require("lazyvim.config")
local Util = require("lazyvim.util") local Util = require("lazyvim.util")
---@class lazyvim.util.json ---@class lazyvim.util.json
@ -45,4 +46,33 @@ function M.encode(value)
return encode(value, "") return encode(value, "")
end end
function M.save()
local path = vim.fn.stdpath("config") .. "/lazyvim.json"
local f = io.open(path, "w")
if f then
f:write(Util.json.encode(Config.json.data))
f:close()
end
end
function M.migrate()
Util.info("Migrating `lazyvim.json` to version `" .. Config.json.version .. "`")
local json = Config.json
-- v0
if not json.data.version then
if json.data.hashes then
---@diagnostic disable-next-line: no-unknown
json.data.hashes = nil
end
json.data.extras = vim.tbl_map(function(extra)
return "lazyvim.plugins.extras." .. extra
end, json.data.extras or {})
end
json.data.version = Config.json.version
M.save()
end
return M return M

View File

@ -60,7 +60,7 @@ function M.open(file, opts)
return return
end end
Config.json.data.news[ref] = hash Config.json.data.news[ref] = hash
Config.json.save() Util.json.save()
end end
local float = require("lazy.util").float({ local float = require("lazy.util").float({