Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: included caching in add-new-version.js. #552

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions scripts/add-new-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ const fs = require('fs');
const inputNewVersion = process.env.newVersion;
//Regex taken from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const versionRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/g; //NOSONAR
const cachedFiles = {};

function readJsonFile(filePath){
if(!cachedFiles[filePath]){
cachedFiles[filePath] = JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
return cachedFiles[filePath];
}

function writeJsonFile(filePath){
if(cachedFiles[filePath]){
fs.writeFileSync(filePath, JSON.stringify(cachedFiles[filePath], null, 2));
}
}

/**
* Promise based function to execute commands
Expand Down Expand Up @@ -39,7 +53,7 @@ function addNewSchemaVersion(newVersion, newVersionDir, latestVersion) {
//Did the major version (first char) change from last to new version?
const isMajorVersionChange = newVersion.charAt(0) !== latestVersion.charAt(0);
const objFile = path.resolve(newVersionDir, 'multiFormatSchema.json');
const obj = require(objFile);
const obj = readJsonFile(objFile);

// Adapt all the MUST supported schema formats
let mustSupportedSchemaFormats = obj && obj.else && obj.else.properties && obj.else.properties.schemaFormat && obj.else.properties.schemaFormat.anyOf && obj.else.properties.schemaFormat.anyOf[1] && obj.else.properties.schemaFormat.anyOf[1].enum ? obj.else.properties.schemaFormat.anyOf[1].enum : [];
Expand Down Expand Up @@ -68,18 +82,18 @@ function addNewSchemaVersion(newVersion, newVersionDir, latestVersion) {
throw new Error('Could not find location for schemaFormats that applies the AsyncAPI Schema object to the schema property');
}

fs.writeFileSync(objFile, JSON.stringify(obj, null, 2));
cachedFiles[objFile] = obj;
}

/**
* Adapt the root title and .asyncapi property
*/
function adaptRootObject(newVersion, newVersionDir) {
const objFile = path.resolve(newVersionDir, 'asyncapi.json');
const obj = require(objFile);
const obj = readJsonFile(objFile);
obj.title = `AsyncAPI ${newVersion} schema.`;
obj.properties.asyncapi.const = newVersion;
fs.writeFileSync(objFile, JSON.stringify(obj, null, 2));
cachedFiles[objFile] = obj;
}

async function addNewVersion(newVersion) {
Expand Down Expand Up @@ -111,6 +125,8 @@ async function addNewVersion(newVersion) {
// Add new schemaFormat version entries
addNewSchemaVersion(newVersion, newVersionDir, latestVersion);

Object.keys(cachedFiles).forEach(writeJsonFile);

console.log(`New version added to ${newVersionDir}`);
}

Expand Down
Loading