use plop.getGenerator

This commit is contained in:
Dieter Stinglhamber 2021-11-08 11:31:37 +01:00
parent e9f729a664
commit 6a549e4843
2 changed files with 67 additions and 84 deletions

View File

@ -3,8 +3,6 @@
const { join } = require('path');
const fs = require('fs-extra');
const validateInput = require('./utils/validate-input');
const contentTypePrompts = require('./content-type').prompts;
const contentTypeActions = require('./content-type').actions;
module.exports = plop => {
// API generator
@ -74,12 +72,10 @@ module.exports = plop => {
return api;
}
// TODO: make prompts and actions more re-usable and composable
const contentType = await contentTypePrompts(plop, inquirer);
return {
...api,
...contentType,
// TODO: make prompts and actions more re-usable and composable
...(await plop.getGenerator('content-type').prompts(inquirer)),
};
},
actions(answers) {
@ -125,7 +121,7 @@ module.exports = plop => {
},
...baseActions,
// TODO: make prompts and actions more re-usable and composable
...(answers.createContentType ? contentTypeActions(answers) : []),
...(answers.createContentType ? plop.getGenerator('content-type').actions(answers) : []),
];
},
});

View File

@ -133,8 +133,11 @@ const promptAttributeQuestions = inquirer => {
]);
};
// TODO: make prompts and actions more re-usable and composable
const prompts = async (plop, inquirer) => {
module.exports = plop => {
// Model generator
plop.setGenerator('content-type', {
description: 'Generate a content type for an API',
async prompts(inquirer) {
const config = await promptConfigQuestions(plop, inquirer);
if (!config.addAttributes) {
@ -162,10 +165,8 @@ const prompts = async (plop, inquirer) => {
...config,
attributes,
};
};
// TODO: make prompts and actions more re-usable and composable
const actions = answers => {
},
actions(answers) {
const attributes = answers.attributes.reduce((object, answer) => {
const val = { type: answer.attributeType };
@ -203,20 +204,6 @@ const actions = answers => {
},
},
];
};
module.exports = plop => {
// Model generator
plop.setGenerator('content-type', {
description: 'Generate a content type for an API',
async prompts(inquirer) {
return prompts(plop, inquirer);
},
actions(answers) {
return actions(answers);
},
});
};
module.exports.prompts = prompts;
module.exports.actions = actions;