Skip to content

Commit

Permalink
feat: suggest possible correct parameter name (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored May 25, 2020
1 parent fc4dee9 commit 382bb8c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/templateConfigValidator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const semver = require('semver');
const Ajv = require('ajv');
const { getGeneratorVersion } = require('./utils');
const levenshtein = require('levenshtein-edit-distance');

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

Expand Down Expand Up @@ -57,6 +58,21 @@ function isRequiredParamProvided(configParams, templateParams) {
}
}

/**
* Provides a hint for a user about correct parameter name.
* @private
* @param {Object} wrongParams Incorrectly written parameters
* @param {Object} configParams Parameters specified in template configuration
*/
function provideAdvice(wrongParams, configParams) {
if (configParams) {
wrongParams.forEach(wp => console.warn(`Did you mean "${Object.keys(configParams)
.map(param => [levenshtein(wp, param), param]).sort()[0][1]}"?`));
} else {
console.warn('This template doesn\'t have any params!');
}
}

/**
* Checks if parameters provided to generator is supported by the template
* @private
Expand All @@ -68,6 +84,7 @@ function isProvidedParameterSupported(configParams, templateParams) {

if (wrongParams.length) {
console.warn(`Warning: This template doesn't have the following params: ${wrongParams}.`);
provideAdvice(wrongParams, configParams);
}
}

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"npmi": "^4.0.0",
"nunjucks": "^3.2.0",
"semver": "^7.3.2",
"simple-git": "^1.131.0"
"simple-git": "^1.131.0",
"levenshtein-edit-distance": "^2.0.5"
},
"devDependencies": {
"@semantic-release/commit-analyzer": "^8.0.1",
Expand Down
2 changes: 2 additions & 0 deletions test/templateConfigValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('Template Configuration Validator', () => {
};
validateTemplateConfig(templateConfig, templateParams);
expect(console.warn).toHaveBeenCalledWith('Warning: This template doesn\'t have the following params: test1.');
expect(console.warn).toHaveBeenCalledWith('Did you mean "test"?');
});

it('Validation throw error if provided param is not supported by the template as template has no params specified', () => {
Expand All @@ -73,6 +74,7 @@ describe('Template Configuration Validator', () => {

validateTemplateConfig(templateConfig, templateParams);
expect(console.warn).toHaveBeenCalledWith('Warning: This template doesn\'t have the following params: test1.');
expect(console.warn).toHaveBeenCalledWith('This template doesn\'t have any params!');
});

it('Validation throw error if specified server is not in asyncapi document', () => {
Expand Down

0 comments on commit 382bb8c

Please sign in to comment.