diff --git a/lib/generator.js b/lib/generator.js index 40c0687ae..3a463d8ca 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -643,7 +643,6 @@ class Generator { this.templateConfig = {}; } await this.loadDefaultValues(); - await this.validateTemplateConfig(); } /** @@ -680,12 +679,7 @@ class Generator { throw new Error(`This template is not compatible with the current version of the generator (${packageJson.version}). This template is compatible with the following version range: ${generator}.`); } - const requiredParams = Object.keys(parameters || {}).filter(key => parameters[key].required === true); - - const missingParams = requiredParams.filter(rp => this.templateParams[rp] === undefined); - if (missingParams.length) { - throw new Error(`This template requires the following missing params: ${missingParams}.`); - } + this.verifyParameters(parameters); if (typeof conditionalFiles === 'object') { const fileNames = Object.keys(conditionalFiles) || []; @@ -709,6 +703,25 @@ class Generator { } } + /** + * Checks that all required parameters are set and all specified by user parameters are present in template. + * + * @private + * @param {Array} [parameters] parameters known from template configuration. + */ + verifyParameters(parameters) { + const missingParams = Object.keys(parameters || {}) + .filter(key => parameters[key].required === true && this.templateParams[key] === undefined); + if (missingParams.length) { + throw new Error(`This template requires the following missing params: ${missingParams}.`); + } + + const wrongParams = Object.keys(this.templateParams || {}).filter(key => parameters[key] === undefined); + if (wrongParams.length) { + console.warn(`Warning: This template doesn't have the following params: ${wrongParams}.`); + } + } + /** * Launches all the hooks registered at a given hook point/name. * diff --git a/test/generator.test.js b/test/generator.test.js index a60a7ccad..5ab7140df 100644 --- a/test/generator.test.js +++ b/test/generator.test.js @@ -466,4 +466,44 @@ describe('Generator', () => { }); }); }); + + describe('#verifyParameters', () => { + it('required parameter is missed', () => { + const gen = new Generator('testTemplate', __dirname, { + templateParams: { + test: true + } + }); + const params = { + requiredParam: { + required: true + }, + test: { + required: false + } + }; + + expect(() => { + gen.verifyParameters(params); + }).toThrow(/requiredParam/); + }); + it('wrong parameter is passed', () => { + const gen = new Generator('testTemplate', __dirname, { + templateParams: { + notExistInConf: true + } + }); + const params = { + existInConf: { + required: false + } + }; + console.warn = jest.fn(); + + gen.verifyParameters(params); + + expect(console.warn).toBeCalledWith(expect.stringContaining('notExistInConf')); + expect(console.warn).toBeCalledWith(expect.not.stringContaining('existInConf')); + }); + }); });