|
| 1 | +const path = require("path"); |
| 2 | +const util = require("util"); |
| 3 | +const fs = require("fs"); |
| 4 | +const Bundler = require("@hyperjump/json-schema-bundle"); |
| 5 | +const resolveUri = require("@jridgewell/resolve-uri"); |
| 6 | +const parseURI = require("parse-uri"); |
| 7 | + |
| 8 | +const readFile = util.promisify(fs.readFile); |
| 9 | +const writeFile = util.promisify(fs.writeFile); |
| 10 | +const mkdir = util.promisify(fs.mkdir); |
| 11 | +const glob = util.promisify(require("glob")); |
| 12 | + |
| 13 | +function normalizeRef(ref) { |
| 14 | + const uri = parseURI(ref); |
| 15 | + return uri.relative |
| 16 | + .replace(/^\/mason-registry\.json\//, "") |
| 17 | + .replaceAll("/", ":"); |
| 18 | +} |
| 19 | + |
| 20 | +function normalizeKeys(schema) { |
| 21 | + for (const key in schema) { |
| 22 | + const new_key = normalizeRef(key); |
| 23 | + schema[new_key] = schema[key]; |
| 24 | + delete schema[new_key].$id; |
| 25 | + delete schema[key]; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function expandRefs(schema, id) { |
| 30 | + if (typeof schema !== "object") { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if (Array.isArray(schema)) { |
| 35 | + for (const item of schema) { |
| 36 | + expandRefs(item, id); |
| 37 | + } |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + for (let [key, value] of Object.entries(schema)) { |
| 42 | + if (key == "$defs") { |
| 43 | + schema.definitions = value; |
| 44 | + delete schema.$defs; |
| 45 | + } else if (key == "$ref") { |
| 46 | + value = value.replace(/\$defs/, "definitions"); |
| 47 | + schema.$ref = resolveUri(value, id); |
| 48 | + } |
| 49 | + |
| 50 | + expandRefs(value, schema.$id ?? id); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function normalizeRefs(schema) { |
| 55 | + if (typeof schema !== "object") { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (Array.isArray(schema)) { |
| 60 | + for (const item of schema) { |
| 61 | + normalizeRefs(item); |
| 62 | + } |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + for (const [key, value] of Object.entries(schema)) { |
| 67 | + delete value.$id; |
| 68 | + if (key == "$ref") { |
| 69 | + const uri = parseURI(value); |
| 70 | + schema.$ref = "#/definitions/" + normalizeRef(uri.path) + uri.anchor; |
| 71 | + } else { |
| 72 | + normalizeRefs(value); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function draft07Compat(schema) { |
| 78 | + const originalId = schema.$id; |
| 79 | + expandRefs(schema, originalId); |
| 80 | + normalizeKeys(schema.definitions); |
| 81 | + normalizeRefs(schema); |
| 82 | + schema.$id = originalId; |
| 83 | + schema.$schema = "http://json-schema.org/draft-07/schema#"; |
| 84 | +} |
| 85 | + |
| 86 | +async function main() { |
| 87 | + for (const schema of await glob( |
| 88 | + path.join( |
| 89 | + path.resolve(__dirname, "schemas"), |
| 90 | + "{components,enums}/**/*.json", |
| 91 | + ), |
| 92 | + {}, |
| 93 | + )) { |
| 94 | + console.log("Adding schema", schema); |
| 95 | + Bundler.add(JSON.parse(await readFile(schema))); |
| 96 | + } |
| 97 | + |
| 98 | + const main = await Bundler.get( |
| 99 | + `file://${path.resolve(__dirname, "schemas", "package.schema.json")}`, |
| 100 | + ); |
| 101 | + |
| 102 | + console.log("Bundling…"); |
| 103 | + const schema = await Bundler.bundle(main); |
| 104 | + draft07Compat(schema); |
| 105 | + |
| 106 | + const outDir = path.resolve(__dirname, "out"); |
| 107 | + await mkdir(outDir); |
| 108 | + await writeFile( |
| 109 | + path.resolve(outDir, "package.schema.json"), |
| 110 | + JSON.stringify(schema, null, 2), |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +main().catch((e) => { |
| 115 | + console.error(e); |
| 116 | + process.exit(1); |
| 117 | +}); |
0 commit comments