Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into rishabhescape
Browse files Browse the repository at this point in the history
rishabhr4 authored Oct 18, 2024
2 parents b5ae112 + 25c4e53 commit 5573e4e
Showing 6 changed files with 41 additions and 38 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**
8 changes: 4 additions & 4 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ class Generator {
Object.defineProperty(this.templateParams, key, {
enumerable: true,
get() {
if (!self.templateConfig.parameters || !self.templateConfig.parameters[key]) {
if (!self.templateConfig.parameters?.[key]) {
throw new Error(`Template parameter "${key}" has not been defined in the package.json file under generator property. Please make sure it's listed there before you use it in your template.`);
}
return templateParams[key];
@@ -562,8 +562,8 @@ class Generator {

try {
installedPkg = getTemplateDetails(this.templateName, PACKAGE_JSON_FILENAME);
pkgPath = installedPkg && installedPkg.pkgPath;
packageVersion = installedPkg && installedPkg.version;
pkgPath = installedPkg?.pkgPath;
packageVersion = installedPkg?.version;
log.debug(logMessage.templateSource(pkgPath));
if (packageVersion) log.debug(logMessage.templateVersion(packageVersion));

@@ -759,7 +759,7 @@ class Generator {
// Check if the filename dictates it should be separated
let wasSeparated = false;
for (const prop in fileNamesForSeparation) {
if (Object.prototype.hasOwnProperty.call(fileNamesForSeparation, prop) && stats.name.includes(`$$${prop}$$`)) {
if (Object.hasOwn(fileNamesForSeparation, prop) && stats.name.includes(`$$${prop}$$`)) {
await this.generateSeparateFiles(asyncapiDocument, fileNamesForSeparation[prop], prop, stats.name, root);
const templateFilePath = path.relative(this.templateContentDir, path.resolve(root, stats.name));
fs.unlink(path.resolve(this.targetDir, templateFilePath), next);
4 changes: 2 additions & 2 deletions apps/generator/lib/logMessages.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ const TEMPLATE_INSTALL_FLAG_MSG = 'because you passed --install flag';

const TEMPLATE_INSTALL_DISK_MSG = 'because the template cannot be found on disk';

const NODE_MODULES_INSTALL ='Remember that your local template must have its own node_modules installed first, \"npm install\" is not triggered by the generator.';
const NODE_MODULES_INSTALL = 'Remember that your local template must have its own node_modules installed first, "npm install" is not triggered by the generator.';

const NPM_INSTALL_TRIGGER = 'Installation of template located on disk technically means symlink creation betweed node_modules of the generator and template sources. Your local template must have its own node_modules, "npm install" is not triggered.';

@@ -19,7 +19,7 @@ function templateNotFound(templateName) {
}

function packageNotAvailable(packageDetails) {
if (packageDetails && packageDetails.pkgPath) {
if (packageDetails?.pkgPath) {
return `Unable to resolve template location at ${packageDetails.pkgPath}. Package is not available locally.`;
}

2 changes: 1 addition & 1 deletion apps/generator/lib/templateConfigValidator.js
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ function getParamSuggestion(wrongParam, configParams) {
* @param {Object} templateParams All parameters provided to generator
*/
function isProvidedParameterSupported(configParams, templateParams) {
const wrongParams = Object.keys(templateParams || {}).filter(key => !configParams || !configParams[key]);
const wrongParams = Object.keys(templateParams || {}).filter(key => !configParams?.[key]);

if (!wrongParams.length) return;
if (!configParams) throw new Error('This template doesn\'t have any params.');
4 changes: 2 additions & 2 deletions apps/generator/test/renderer.test.js
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@ jest.mock('@asyncapi/generator-react-sdk');

describe('React renderer', () => {
describe('saveRenderedReactContent', () => {
let util = undefined;
let AsyncReactSDK = undefined;
let util;
let AsyncReactSDK;
beforeAll(() => {
util = require('../lib/utils');
AsyncReactSDK = require('@asyncapi/generator-react-sdk');
60 changes: 31 additions & 29 deletions apps/nunjucks-filters/src/customFilters.js
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ function getPayloadExamples(msg) {
}

const payload = msg.payload();
if (payload && payload.examples()) {
if (payload?.examples()) {
return payload.examples().map(example => ({ example }));
}
}
@@ -90,7 +90,7 @@ function getHeadersExamples(msg) {
}

const headers = msg.headers();
if (headers && headers.examples()) {
if (headers?.examples()) {
return headers.examples().map(example => ({ example }));
}
}
@@ -120,7 +120,7 @@ filter.oneLine = oneLine;

/**
* Generate JSDoc from message properties of the header and the payload
*
*
* @example
* docline(
* Schema {
@@ -134,52 +134,54 @@ filter.oneLine = oneLine;
* my-app-header,
* options.message.headers
* )
*
*
* Returned value will be -> * @param {integer} options.message.headers.my-app-header
*
*
* @field {object} - Property object
* @fieldName {string} - Name of documented property
* @scopePropName {string} - Name of param for JSDocs
* @returns {string} JSDoc compatible entry
*/
function docline(field, fieldName, scopePropName) {
/* eslint-disable sonarjs/cognitive-complexity */
const getType = (f) => f.type() || 'string';
const getDescription = (f) => f.description() ? ` - ${f.description().replace(/\r?\n|\r/g, '')}` : '';
const getDefault = (f, type) => (f.default() && type === 'string') ? `'${f.default()}'` : f.default();
const getPName = (pName) => pName ? `${pName}.` : '';

const buildLineCore = (type, def, pName, fName) => {
const paramName = `${pName}${fName}`;
const defaultValue = def !== undefined ? `=${def}` : '';
return `* @param {${type}} ${paramName}${defaultValue}`;
};

const buildLine = (f, fName, pName) => {
const type = f.type() ? f.type() : 'string';
const description = f.description() ? ` - ${f.description().replace(/\r?\n|\r/g, '')}` : '';
let def = f.default();

if (def && type === 'string') def = `'${def}'`;

let line;
if (def !== undefined) {
line = ` * @param {${type}} [${pName ? `${pName}.` : ''}${fName}=${def}]`;
} else {
line = ` * @param {${type}} ${pName ? `${pName}.` : ''}${fName}`;
}
const type = getType(f);
const def = getDefault(f, type);
const line = buildLineCore(type, def, getPName(pName), fName);
return line + (type === 'object' ? '' : getDescription(f));
};

if (type === 'object') {
let lines = `${line}\n`;
let first = true;
for (const propName in f.properties()) {
lines = `${lines}${first ? '' : '\n'}${buildLine(f.properties()[propName], propName, `${pName ? `${pName}.` : ''}${fName}`)}`;
first = false;
}
return lines;
}
const buildObjectLines = (f, fName, pName) => {
const properties = f.properties();
const mainLine = buildLine(f, fName, pName);

return `${line}${description}`;
return `${mainLine }\n${ Object.keys(properties).map((propName) =>
buildLine(properties[propName], propName, `${getPName(pName)}${fName}`)
).join('\n')}`;
};

return buildLine(field, fieldName, scopePropName);
return getType(field) === 'object'
? buildObjectLines(field, fieldName, scopePropName)
: buildLine(field, fieldName, scopePropName);
}
filter.docline = docline;

/**
* Helper function to replace server variables in the url with actual values
* @url {string} - url string
* @serverserverVariables {Object} - Variables model map
* @returns {string}
* @returns {string}
*/
function replaceServerVariablesWithValues(url, serverVariables) {
const getVariablesNamesFromUrl = (inputUrl) => {

0 comments on commit 5573e4e

Please sign in to comment.