Skip to content

Commit

Permalink
add input validationn to basic cli creation mode/
Browse files Browse the repository at this point in the history
  • Loading branch information
galElmalah committed Sep 1, 2020
1 parent 51a82cf commit a3b21ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Binary file modified .DS_Store
Binary file not shown.
36 changes: 27 additions & 9 deletions src/cliCommandHandlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,32 @@ const {
const { interactiveCreateCommandHandler, } = require('./interactiveCreateHandler');
const { getTemplateHooksFromConfig } = require('./getTemplateHooksFromConfig');
const { asyncExecutor } = require('./asyncExecutor');
const { getValidationFunction } = require('./questions');

const validateParametersValues = (parametersOptions,keyValuePairs) => {
for(const [parameter,value] of Object.entries(keyValuePairs)) {
const validationFn = getValidationFunction(parametersOptions,parameter);
if(validationFn) {
const res = validationFn(value);
if(typeof res ==='string') {
throw new Error(`invalid value for "${parameter}"::${res}`);
}
}
}
};


const getTransformedTemplates = (command, cmd) => {
const commandsLocations = commandsBuilder(cmd.loadFrom || process.cwd());

const { config, currentCommandTemplate } = templateReader(commandsLocations)(
command
);


const keyValuePairs = generateKeyValues(cmd);

validateParametersValues(config.parametersOptions, keyValuePairs);

const globalCtx = {
templateName: command,
Expand Down Expand Up @@ -73,15 +90,16 @@ const createCommandHandler = async (command, cmd) => {
globalCtx
);

return Promise.all(templatesBuilder.build()).then(() => {
showSuccessMessage(command, templatesBuilder.getFullPath());
asyncExecutor(
postTemplateGeneration,
`Executed "${command}" post-template generation hook.`,
(e) => `Error while Executing "${command}" post-template generation hook::\n${e}`,
globalCtx
);
});
await Promise.all(templatesBuilder.build());

showSuccessMessage(command, templatesBuilder.getFullPath());
asyncExecutor(
postTemplateGeneration,
`Executed "${command}" post-template generation hook.`,
(e) => `Error while Executing "${command}" post-template generation hook::\n${e}`,
globalCtx
);


} catch (err) {
handleError(err);
Expand Down

0 comments on commit a3b21ab

Please sign in to comment.