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 { join } = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const validateInput = require('./utils/validate-input'); const validateInput = require('./utils/validate-input');
const contentTypePrompts = require('./content-type').prompts;
const contentTypeActions = require('./content-type').actions;
module.exports = plop => { module.exports = plop => {
// API generator // API generator
plop.setGenerator('api', { plop.setGenerator('api', {
description: 'Generate a basic API', description: 'Generate a basic API',
prompts: [ async prompts(inquirer) {
const api = await inquirer.prompt([
{ {
type: 'input', type: 'input',
name: 'id', name: 'id',
@ -45,12 +48,12 @@ module.exports = plop => {
}, },
{ {
type: 'list', type: 'list',
name: 'kind', name: 'modelType',
message: 'Please choose the model type', message: 'Please choose the model type',
default: 'collectionType', default: 'collectionType',
choices: [ choices: [
{ name: 'Collection Type', value: 'collectionType' }, { name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' }, { name: 'Single Type', value: 'singleType' },
], ],
}, },
{ {
@ -59,7 +62,26 @@ module.exports = plop => {
default: false, default: false,
message: 'Use draft and publish?', 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) { actions(answers) {
let filePath; let filePath;
if (answers.isPluginApi && answers.plugin) { if (answers.isPluginApi && answers.plugin) {
@ -91,7 +113,7 @@ module.exports = plop => {
} }
const routeType = const routeType =
answers.kind === 'singleType' answers.modelType === 'singleType'
? 'single-type-routes.js.hbs' ? 'single-type-routes.js.hbs'
: 'collection-type-routes.js.hbs'; : 'collection-type-routes.js.hbs';
@ -102,6 +124,8 @@ module.exports = plop => {
templateFile: `templates/${routeType}`, templateFile: `templates/${routeType}`,
}, },
...baseActions, ...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', default: 'collectionType',
choices: [ choices: [
{ name: 'Collection Type', value: 'collectionType' }, { name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' }, { name: 'Single Type', value: 'singleType' },
], ],
validate: input => validateInput(input), validate: input => validateInput(input),
}, },
@ -133,11 +133,8 @@ const promptAttributeQuestions = inquirer => {
]); ]);
}; };
module.exports = plop => { // TODO: make prompts and actions more re-usable and composable
// Model generator const prompts = async (plop, inquirer) => {
plop.setGenerator('content-type', {
description: 'Generate a content type for an API',
async prompts(inquirer) {
const config = await promptConfigQuestions(plop, inquirer); const config = await promptConfigQuestions(plop, inquirer);
if (!config.addAttributes) { if (!config.addAttributes) {
@ -165,8 +162,10 @@ module.exports = plop => {
...config, ...config,
attributes, attributes,
}; };
}, };
actions(answers) {
// TODO: make prompts and actions more re-usable and composable
const actions = answers => {
const attributes = answers.attributes.reduce((object, answer) => { const attributes = answers.attributes.reduce((object, answer) => {
const val = { type: answer.attributeType }; 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;