-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build class to generate json schemas
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
local util = require("lsp-settings.util") | ||
|
||
local M = {} | ||
|
||
function M.get_schema(package_json) | ||
local json = util.json_decode(util.fetch(package_json)) or {} | ||
local config = json.contributes and json.contributes.configuration or json.properties and json | ||
|
||
local properties = {} | ||
|
||
if vim.tbl_islist(config) then | ||
for _, c in pairs(config) do | ||
vim.list_extend(properties, c.properties) | ||
end | ||
elseif config.properties then | ||
properties = config.properties | ||
end | ||
return { | ||
["$schema"] = "http://json-schema.org/draft-07/schema#", | ||
description = json.description, | ||
properties = properties, | ||
} | ||
end | ||
|
||
function M.clean() | ||
---@diagnostic disable-next-line: param-type-mismatch | ||
for _, f in pairs(vim.fn.expand("schemas/*.json", false, true)) do | ||
vim.loop.fs_unlink(f) | ||
end | ||
end | ||
|
||
function M.update_index() | ||
local url = "https://gist.githubusercontent.mirror.nvdadr.com/williamboman/a01c3ce1884d4b57cc93422e7eae7702/raw/lsp-packages.json" | ||
local index = util.fetch(url) | ||
util.write_file("schemas/index.json", index) | ||
util.write_file("lua/lsp-settings/schemas.lua", "return " .. vim.inspect(util.json_decode(index))) | ||
end | ||
|
||
function M.index() | ||
return util.json_decode(util.read_file("schemas/index.json")) | ||
end | ||
|
||
function M.update_schemas() | ||
for name, url in pairs(M.index()) do | ||
print(("Generating schema for %s"):format(name)) | ||
local schema_file = ("schemas/%s.json"):format(name) | ||
|
||
if not (util.exists(schema_file) and os.time() - util.mtime(schema_file) < 3600) then | ||
local schema = M.get_schema(url) | ||
util.write_file(schema_file, vim.fn.json_encode(schema)) | ||
end | ||
end | ||
end | ||
|
||
function M.build() | ||
-- M.clean() | ||
M.update_index() | ||
M.update_schemas() | ||
end | ||
|
||
M.build() | ||
|
||
return M |