Skip to content

Commit

Permalink
remove unnecessary formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg authored Apr 18, 2024
1 parent cea15ad commit 6954bdb
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions tools/bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ const extensionsRegex = /http:\/\/asyncapi\.com\/(extensions\/[^/]+)\/([^/]+)\/(
if (!fs.existsSync(outputDirectory)) {
await fs.promises.mkdir(outputDirectory);
}
console.log(
`The following versions have separate definitions: ${versions.join(',')}`
);
console.log(`The following versions have separate definitions: ${versions.join(',')}`);
for (const version of versions) {
const Bundler = require('@hyperjump/json-schema-bundle');
try {
console.log(`Bundling the following version together: ${version}`);
const outputFileWithId = path.resolve(outputDirectory, `${version}.json`);
const outputFileWithoutId = path.resolve(
outputDirectory,
`${version}-without-$id.json`
);
const outputFileWithoutId = path.resolve(outputDirectory, `${version}-without-$id.json`);
const versionDir = path.resolve(definitionsDirectory, version);
await loadDefinitions(Bundler, versionDir);
await loadCommonSchemas(Bundler);
Expand All @@ -62,24 +57,15 @@ const extensionsRegex = /http:\/\/asyncapi\.com\/(extensions\/[^/]+)\/([^/]+)\/(
: ''
}`;
console.log(`Writing the bundled file WITH $ids to: ${outputFileWithId}`);
await fs.promises.writeFile(
outputFileWithId,
JSON.stringify(bundledSchemaWithId, null, 4)
);
await fs.promises.writeFile(outputFileWithId, JSON.stringify(bundledSchemaWithId, null, 4));

/**
* removing ids from schemas and making modifications in definitions name to make sure schemas still work
* this is needed for tools that do not support $id feature in JSON Schema
*/
const bundledSchemaWithoutIds =
modifyRefsAndDefinitions(bundledSchemaWithId);
console.log(
`Writing the bundled file WITHOUT $ids to: ${outputFileWithoutId}`
);
await fs.promises.writeFile(
outputFileWithoutId,
JSON.stringify(bundledSchemaWithoutIds, null, 4)
);
const bundledSchemaWithoutIds = modifyRefsAndDefinitions(bundledSchemaWithId);
console.log(`Writing the bundled file WITHOUT $ids to: ${outputFileWithoutId}`);
await fs.promises.writeFile(outputFileWithoutId, JSON.stringify(bundledSchemaWithoutIds, null, 4));
} catch (e) {
throw new Error(e);
}
Expand Down Expand Up @@ -203,10 +189,7 @@ function modifyRefsAndDefinitions(bundledSchema) {
traverse(bundledSchema, replaceRef);
traverse(bundledSchema.definitions.avroSchema_v1, updateAvro);
traverse(bundledSchema.definitions.openapiSchema_3_0, updateOpenApi);
traverse(
bundledSchema.definitions['json-schema-draft-07-schema'],
updateJsonSchema
);
traverse(bundledSchema.definitions['json-schema-draft-07-schema'], updateJsonSchema);

return bundledSchema;
}
Expand Down Expand Up @@ -238,9 +221,7 @@ function getDefinitionName(def) {
return `${result[1].replace('/', '-')}-${result[2]}-${result[3]}`;
}

return `${result[1].replace('/', '-')}-${result[2]}-${
result[3]
}/${result[4].replace('#/', '')}`;
return `${result[1].replace('/', '-')}-${result[2]}-${result[3]}/${result[4].replace('#/', '')}`;
}
}
if (def.startsWith('http://asyncapi.com/extensions')) {
Expand All @@ -260,19 +241,18 @@ function replaceRef(schema) {
//new refs will only work if we remove $id that all point to asyncapi.com
delete schema.$id;

//traversing shoudl take place only in case of schemas with refs
//traversing should take place only in case of schemas with refs
if (schema.$ref === undefined) return;
// updating refs that are related to remote URL refs that need to be update and point to inlined versions
if (!schema.$ref.startsWith('#'))
schema.$ref = `#/definitions/${getDefinitionName(schema.$ref)}`;
if (!schema.$ref.startsWith('#')) schema.$ref = `#/definitions/${getDefinitionName(schema.$ref)}`;
}

/**
* this is a callback used when traversing through json schema
* to fix avro schema definitions to point to right direction
*/
function updateAvro(schema) {
//traversing shoudl take place only in case of schemas with refs
//traversing should take place only in case of schemas with refs
if (schema.$ref === undefined) return;

schema.$ref = schema.$ref.replace(
Expand All @@ -287,7 +267,7 @@ function updateAvro(schema) {
* to fix open api schema definitions to point to right direction
*/
function updateOpenApi(schema) {
//traversing shoudl take place only in case of schemas with refs
//traversing should take place only in case of schemas with refs
if (schema.$ref === undefined) return;
const openApiPropName = 'openapiSchema_3_0';

Expand All @@ -307,7 +287,7 @@ function updateOpenApi(schema) {
* to fix open api schema definitions to point to right direction
*/
function updateJsonSchema(schema) {
//traversing shoudl take place only in case of schemas with refs
//traversing should take place only in case of schemas with refs
if (schema.$ref === undefined) return;

schema.$ref = schema.$ref.replace(
Expand Down

0 comments on commit 6954bdb

Please sign in to comment.