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

feat: show latest compatible generator version in template compatibility errors #1355

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 19 additions & 3 deletions apps/generator/lib/templateConfigValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const levenshtein = require('levenshtein-edit-distance');
// eslint-disable-next-line no-unused-vars
const {AsyncAPIDocumentInterface, AsyncAPIDocument} = require('@asyncapi/parser');
const { usesNewAPI } = require('./parser');
const { getVersionsList } = require('./versions');

const ajv = new Ajv({ allErrors: true });

Expand All @@ -23,7 +24,7 @@ const supportedParserAPIMajorVersions = [
* @param {AsyncAPIDocumentInterface | AsyncAPIDocument} asyncapiDocument AsyncAPIDocument object to use as source.
* @return {Boolean}
*/
module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncapiDocument) => {
module.exports.validateTemplateConfig = async(templateConfig, templateParams, asyncapiDocument) => {
const { parameters, supportedProtocols, conditionalFiles, generator, apiVersion } = templateConfig;

validateConditionalFiles(conditionalFiles);
Expand Down Expand Up @@ -51,10 +52,25 @@ module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncap
* @param {String} generator Information about supported generator version that is part of the template configuration
* @param {String} generator Information about supported Parser-API version that is part of the template configuration
*/
function isTemplateCompatible(generator, apiVersion) {
async function isTemplateCompatible(generator, apiVersion) {
const generatorVersion = getGeneratorVersion();
if (typeof generator === 'string' && !semver.satisfies(generatorVersion, generator, {includePrerelease: true})) {
throw new Error(`This template is not compatible with the current version of the generator (${generatorVersion}). This template is compatible with the following version range: ${generator}.`);
const allVersions = await getVersionsList();
const compatibleVersions = allVersions
.filter(version => semver.satisfies(version, generator, {includePrerelease: true}))
.sort(semver.compare);

const latestCompatibleVersion = compatibleVersions.length > 0
? compatibleVersions[compatibleVersions.length - 1]
: null;

let errorMessage = `This template is not compatible with the current version of the generator (${generatorVersion}). `;
errorMessage += `This template is compatible with the following version range: ${generator}`;

if (latestCompatibleVersion) {
errorMessage += `\nTo use this template, please install generator version ${latestCompatibleVersion} using:\nnpm install -g @asyncapi/generator@${latestCompatibleVersion}`;
}
throw new Error(errorMessage);
}

if (typeof apiVersion === 'string' && !supportedParserAPIMajorVersions.includes(apiVersion)) {
Expand Down
32 changes: 32 additions & 0 deletions apps/generator/lib/versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const https = require('https');

async function getVersionsList() {
try {
const versions = await fetchNpmVersions('@asyncapi/generator');
return versions;
} catch (error) {
const { getGeneratorVersion } = require('./utils');
return [getGeneratorVersion()];
}
}

function fetchNpmVersions(packageName) {
return new Promise((resolve, reject) => {
https.get(`https://registry.npmjs.org/${packageName}`, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
const versions = Object.keys(JSON.parse(data).versions);
resolve(versions);
} catch (error) {
reject(error);
}
});
}).on('error', reject);
});
}

module.exports = {
getVersionsList
};
Loading