add the content-type generator to the API generator

This commit is contained in:
Dieter Stinglhamber 2021-11-08 10:27:23 +01:00
parent 7f285fb755
commit e9f729a664
2 changed files with 154 additions and 117 deletions

View File

@ -3,12 +3,15 @@
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
plop.setGenerator('api', {
description: 'Generate a basic API',
prompts: [
async prompts(inquirer) {
const api = await inquirer.prompt([
{
type: 'input',
name: 'id',
@ -45,12 +48,12 @@ module.exports = plop => {
},
{
type: 'list',
name: 'kind',
name: 'modelType',
message: 'Please choose the model type',
default: 'collectionType',
choices: [
{ name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' },
{ name: 'Single Type', value: 'singleType' },
],
},
{
@ -59,7 +62,26 @@ module.exports = plop => {
default: false,
message: 'Use draft and publish?',
},
],
{
type: 'confirm',
name: 'createContentType',
default: false,
message: 'Create a content-type?',
},
]);
if (!api.createContentType) {
return api;
}
// TODO: make prompts and actions more re-usable and composable
const contentType = await contentTypePrompts(plop, inquirer);
return {
...api,
...contentType,
};
},
actions(answers) {
let filePath;
if (answers.isPluginApi && answers.plugin) {
@ -91,7 +113,7 @@ module.exports = plop => {
}
const routeType =
answers.kind === 'singleType'
answers.modelType === 'singleType'
? 'single-type-routes.js.hbs'
: 'collection-type-routes.js.hbs';
@ -102,6 +124,8 @@ module.exports = plop => {
templateFile: `templates/${routeType}`,
},
...baseActions,
// TODO: make prompts and actions more re-usable and composable
...(answers.createContentType ? contentTypeActions(answers) : []),
];
},
});

View File

@ -76,7 +76,7 @@ const promptConfigQuestions = (plop, inquirer) => {
default: 'collectionType',
choices: [
{ name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' },
{ name: 'Single Type', value: 'singleType' },
],
validate: input => validateInput(input),
},
@ -133,11 +133,8 @@ const promptAttributeQuestions = inquirer => {
]);
};
module.exports = plop => {
// Model generator
plop.setGenerator('content-type', {
description: 'Generate a content type for an API',
async prompts(inquirer) {
// TODO: make prompts and actions more re-usable and composable
const prompts = async (plop, inquirer) => {
const config = await promptConfigQuestions(plop, inquirer);
if (!config.addAttributes) {
@ -165,8 +162,10 @@ module.exports = plop => {
...config,
attributes,
};
},
actions(answers) {
};
// TODO: make prompts and actions more re-usable and composable
const actions = answers => {
const attributes = answers.attributes.reduce((object, answer) => {
const val = { type: answer.attributeType };
@ -204,6 +203,20 @@ module.exports = plop => {
},
},
];
};
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;