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 warning for wrong spelled params #350

Merged
merged 3 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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'));
});
});
});