Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman committed Jun 5, 2024
0 parents commit a425dac
Show file tree
Hide file tree
Showing 32 changed files with 1,901 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
name: Release

on:
push:
tags:
- "v*.*.*"

jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Bundle schema
shell: bash
run: |
npm install
node bundle.js
- name: Release
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
files: |
out/package.schema.json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
out/
117 changes: 117 additions & 0 deletions bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const path = require("path");
const util = require("util");
const fs = require("fs");
const Bundler = require("@hyperjump/json-schema-bundle");
const resolveUri = require("@jridgewell/resolve-uri");
const parseURI = require("parse-uri");

const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const mkdir = util.promisify(fs.mkdir);
const glob = util.promisify(require("glob"));

function normalizeRef(ref) {
const uri = parseURI(ref);
return uri.relative
.replace(/^\/mason-registry\.json\//, "")
.replaceAll("/", ":");
}

function normalizeKeys(schema) {
for (const key in schema) {
const new_key = normalizeRef(key);
schema[new_key] = schema[key];
delete schema[new_key].$id;
delete schema[key];
}
}

function expandRefs(schema, id) {
if (typeof schema !== "object") {
return;
}

if (Array.isArray(schema)) {
for (const item of schema) {
expandRefs(item, id);
}
return;
}

for (let [key, value] of Object.entries(schema)) {
if (key == "$defs") {
schema.definitions = value;
delete schema.$defs;
} else if (key == "$ref") {
value = value.replace(/\$defs/, "definitions");
schema.$ref = resolveUri(value, id);
}

expandRefs(value, schema.$id ?? id);
}
}

function normalizeRefs(schema) {
if (typeof schema !== "object") {
return;
}

if (Array.isArray(schema)) {
for (const item of schema) {
normalizeRefs(item);
}
return;
}

for (const [key, value] of Object.entries(schema)) {
delete value.$id;
if (key == "$ref") {
const uri = parseURI(value);
schema.$ref = "#/definitions/" + normalizeRef(uri.path) + uri.anchor;
} else {
normalizeRefs(value);
}
}
}

function draft07Compat(schema) {
const originalId = schema.$id;
expandRefs(schema, originalId);
normalizeKeys(schema.definitions);
normalizeRefs(schema);
schema.$id = originalId;
schema.$schema = "http://json-schema.org/draft-07/schema#";
}

async function main() {
for (const schema of await glob(
path.join(
path.resolve(__dirname, "schemas"),
"{components,enums}/**/*.json",
),
{},
)) {
console.log("Adding schema", schema);
Bundler.add(JSON.parse(await readFile(schema)));
}

const main = await Bundler.get(
`file://${path.resolve(__dirname, "schemas", "package.schema.json")}`,
);

console.log("Bundling…");
const schema = await Bundler.bundle(main);
draft07Compat(schema);

const outDir = path.resolve(__dirname, "out");
await mkdir(outDir);
await writeFile(
path.resolve(outDir, "package.schema.json"),
JSON.stringify(schema, null, 2),
);
}

main().catch((e) => {
console.error(e);
process.exit(1);
});
Loading

0 comments on commit a425dac

Please sign in to comment.