Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg committed Dec 13, 2022
1 parent 71431be commit 34f659a
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions tools/bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ console.log(`Using the following output directory: ${outputDirectory}`);
const filePathToBundle = `file://${versionDir}/asyncapi.json`;
const fileToBundle = await Bundler.get(filePathToBundle);
const bundledSchema = await Bundler.bundle(fileToBundle);
const schemaWithoutURLs = modifyRefsandDefinitions(bundledSchema);
schemaWithoutURLs.description = `!!Auto generated!! \n Do not manually edit. ${schemaWithoutURLs.description ?? ''}`;
modifyRefsandDefinitions(bundledSchema);
bundledSchema.description = `!!Auto generated!! \n Do not manually edit. ${bundledSchema.description ?? ''}`;
const outputFile = path.resolve(outputDirectory, `${version}.json`);
console.log(`Writing the bundled file to: ${outputFile}`);
await fs.promises.writeFile(outputFile, JSON.stringify(schemaWithoutURLs, null, 4));
await fs.promises.writeFile(outputFile, JSON.stringify(bundledSchema, null, 4));
}catch(e) {
throw new Error(e);
}
Expand All @@ -49,20 +49,17 @@ console.log(`Using the following output directory: ${outputDirectory}`);
* than update refs to point to new definitions, always inline never remote
*/
function modifyRefsandDefinitions(bundledSchema) {
const schemaWithoutUrls = bundledSchema;

//first we need to improve names of the definitions from URL to their names
for (const def of Object.keys(schemaWithoutUrls.definitions)) {
for (const def of Object.keys(bundledSchema.definitions)) {
const newDefName = getDefinitionName(def);

//creating copy of definition under new name so later definition stored under URL name can be removed
schemaWithoutUrls.definitions[newDefName] = schemaWithoutUrls.definitions[def];
delete schemaWithoutUrls.definitions[def]
bundledSchema.definitions[newDefName] = bundledSchema.definitions[def];
delete bundledSchema.definitions[def]
}

traverse(schemaWithoutUrls, replaceRef);

return schemaWithoutUrls;
traverse(bundledSchema, replaceRef);
}

/**
Expand Down

0 comments on commit 34f659a

Please sign in to comment.