forked from asyncapi/spec-json-schemas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (40 loc) · 2.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const path = require('path');
const fs = require('fs');
const definitionsDirectory = path.resolve(__dirname, '../../definitions');
const outputDirectory = path.resolve(__dirname, '../../schemas');
console.log(`Looking for separate definitions in the following directory: ${definitionsDirectory}`);
console.log(`Using the following output directory: ${outputDirectory}`);
/**
* When run, go through all versions that have split definitions and bundles them together.
*/
(async () => {
const versions = await fs.promises.readdir(definitionsDirectory);
console.log(`Ensuring output directory is present ${outputDirectory}`);
if (!fs.existsSync(outputDirectory)) {
await fs.promises.mkdir(outputDirectory);
}
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 versionDir = path.resolve(definitionsDirectory, version);
const definitions = await fs.promises.readdir(versionDir);
const definitionFiles = definitions.filter((value) => {return !value.includes('asyncapi')}).map((file) => fs.readFileSync(path.resolve(versionDir, file)));
const definitionJson = definitionFiles.map((file) => JSON.parse(file));
for (const jsonFile of definitionJson) {
Bundler.add(jsonFile);
}
const filePathToBundle = `file://${versionDir}/asyncapi.json`;
const fileToBundle = await Bundler.get(filePathToBundle);
const bundledSchema = await Bundler.bundle(fileToBundle);
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(bundledSchema, null, 4));
}catch(e) {
console.log(e);
}
}
console.log('done');
})();