Skip to content

Commit

Permalink
chore: move build schemas/examples to esse class
Browse files Browse the repository at this point in the history
  • Loading branch information
k0stik committed Feb 6, 2024
1 parent 12cf983 commit abb477b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
24 changes: 4 additions & 20 deletions build_schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* downstream consumption to avoid FS calls in the browser.
*/
const fs = require("fs");
const path = require("path");
const mergeAllOf = require("json-schema-merge-allof");
const { ESSE } = require("./lib/js/esse");

// JS Modules
Expand Down Expand Up @@ -43,21 +41,7 @@ if (process.env.BUILD_ASSETS !== "true") {
}

const subfolder = process.env.BUILD_PATH || "./docs/js/";
schemas.forEach((s) => {
let mergedSchema = s;
if (process.env.SKIP_MERGE_ALLOF !== "true") {
mergedSchema = mergeAllOf(s, {
resolvers: { defaultResolver: mergeAllOf.options.resolvers.title },
});
}
const id_as_path = mergedSchema.$id.replace("-", "_");
const full_path = `${subfolder}/schema/${id_as_path}.json`;
fs.mkdirSync(path.dirname(full_path), { recursive: true });
fs.writeFileSync(full_path, JSON.stringify(mergedSchema, null, 4), "utf8");
});
wrappedExamples.forEach((e) => {
const id_as_path = e.path.replace("-", "_");
const full_path = `${subfolder}/example/${id_as_path}.json`;
fs.mkdirSync(path.dirname(full_path), { recursive: true });
fs.writeFileSync(full_path, JSON.stringify(e.data, null, 4), "utf8");
});
const skipMergeAllOff = process.env.SKIP_MERGE_ALLOF === "true";

esse.writeResolvedSchemas(subfolder, skipMergeAllOff);
esse.writeResolvedExamples(subfolder);
27 changes: 27 additions & 0 deletions src/js/esse/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable class-methods-use-this */
import Ajv from "ajv";
import fs from "fs";
import yaml from "js-yaml";
import mergeAllOf from "json-schema-merge-allof";
import path from "path";

import { EXAMPLES_DIR, PROPERTIES_MANIFEST_PATH, SCHEMAS_DIR } from "./settings";
import { parseIncludeReferenceStatementsByDir } from "./utils";
Expand Down Expand Up @@ -39,4 +42,28 @@ export class ESSE {

return isValid;
};

writeResolvedSchemas(subfolder, skipMergeAllOff = false) {
this.schemas.forEach((s) => {
let mergedSchema = s;
if (!skipMergeAllOff) {
mergedSchema = mergeAllOf(s, {
resolvers: { defaultResolver: mergeAllOf.options.resolvers.title },
});
}
const id_as_path = mergedSchema.$id.replace("-", "_");
const full_path = `${subfolder}/schema/${id_as_path}.json`;
fs.mkdirSync(path.dirname(full_path), { recursive: true });
fs.writeFileSync(full_path, JSON.stringify(mergedSchema, null, 4), "utf8");
});
}

writeResolvedExamples(subfolder) {
this.wrappedExamples.forEach((e) => {
const id_as_path = e.path.replace("-", "_");
const full_path = `${subfolder}/example/${id_as_path}.json`;
fs.mkdirSync(path.dirname(full_path), { recursive: true });
fs.writeFileSync(full_path, JSON.stringify(e.data, null, 4), "utf8");
});
}
}

0 comments on commit abb477b

Please sign in to comment.