Skip to content

Commit

Permalink
feat: show warning for wrong spelled params (#350)
Browse files Browse the repository at this point in the history
Co-authored-by: Fran Méndez <[email protected]>
  • Loading branch information
Tenischev and fmvilas authored May 21, 2020
1 parent df2938a commit 2bdd7c3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,6 @@ class Generator {
this.templateConfig = {};
}
await this.loadDefaultValues();
await this.validateTemplateConfig();
}

/**
Expand Down Expand Up @@ -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) || [];
Expand All @@ -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.
*
Expand Down
40 changes: 40 additions & 0 deletions test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});
});

0 comments on commit 2bdd7c3

Please sign in to comment.