mirror of
https://github.com/strapi/strapi.git
synced 2025-09-18 13:02:18 +00:00
rework content-type generator
This commit is contained in:
parent
15d8392f24
commit
c32a1921e9
@ -125,15 +125,14 @@ const createContentType = async ({ contentType, components = [] }, options = {})
|
||||
const generateAPI = ({ singularName, kind = 'collectionType', pluralName, displayName }) => {
|
||||
const strapiGenerators = require('@strapi/generators');
|
||||
return strapiGenerators.generate(
|
||||
'api',
|
||||
'content-type',
|
||||
{
|
||||
id: singularName,
|
||||
kind,
|
||||
singularName,
|
||||
pluralName,
|
||||
displayName,
|
||||
createContentType: true,
|
||||
generateDefaultRoutes: true,
|
||||
destination: 'new',
|
||||
bootstrapApi: true,
|
||||
attributes: [],
|
||||
},
|
||||
{ dir: strapi.dirs.root }
|
||||
|
@ -3,18 +3,12 @@
|
||||
const { join } = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const validateInput = require('./utils/validate-input');
|
||||
const ctNamesPrompts = require('./prompts/ct-names-prompts');
|
||||
const kindPrompts = require('./prompts/kind-prompts');
|
||||
const draftAndPublishPrompts = require('./prompts/draft-and-publish-prompts');
|
||||
const getAttributesPrompts = require('./prompts/get-attributes-prompts');
|
||||
const defaultRoutesPrompts = require('./prompts/default-routes-prompts');
|
||||
|
||||
module.exports = plop => {
|
||||
// API generator
|
||||
plop.setGenerator('api', {
|
||||
description: 'Generate a basic API',
|
||||
async prompts(inquirer) {
|
||||
const api = await inquirer.prompt([
|
||||
prompts: [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'id',
|
||||
@ -55,30 +49,9 @@ module.exports = plop => {
|
||||
default: true,
|
||||
message: 'Create a content-type?',
|
||||
},
|
||||
]);
|
||||
|
||||
if (!api.createContentType) {
|
||||
return api;
|
||||
}
|
||||
|
||||
return {
|
||||
...api,
|
||||
...(await inquirer.prompt([
|
||||
...ctNamesPrompts,
|
||||
...kindPrompts,
|
||||
...draftAndPublishPrompts,
|
||||
...defaultRoutesPrompts,
|
||||
])),
|
||||
attributes: await getAttributesPrompts(inquirer),
|
||||
};
|
||||
},
|
||||
],
|
||||
actions(answers) {
|
||||
let filePath;
|
||||
if (answers.isPluginApi && answers.plugin) {
|
||||
filePath = `plugins/{{plugin}}`;
|
||||
} else {
|
||||
filePath = `api/{{id}}`;
|
||||
}
|
||||
const filePath = answers.isPluginApi && answers.plugin ? 'plugins/{{plugin}}' : 'api/{{id}}';
|
||||
|
||||
const baseActions = [
|
||||
{
|
||||
@ -97,7 +70,6 @@ module.exports = plop => {
|
||||
return baseActions;
|
||||
}
|
||||
|
||||
if (!answers.createContentType) {
|
||||
return [
|
||||
{
|
||||
type: 'add',
|
||||
@ -106,33 +78,6 @@ module.exports = plop => {
|
||||
},
|
||||
...baseActions,
|
||||
];
|
||||
}
|
||||
|
||||
if (answers.generateDefaultRoutes) {
|
||||
const routeType =
|
||||
answers.kind === 'singleType'
|
||||
? 'single-type-routes.js.hbs'
|
||||
: 'collection-type-routes.js.hbs';
|
||||
|
||||
baseActions.push({
|
||||
type: 'add',
|
||||
path: `${filePath}/routes/{{id}}.js`,
|
||||
templateFile: `templates/${routeType}`,
|
||||
});
|
||||
}
|
||||
|
||||
const destination =
|
||||
answers.isPluginApi && answers.plugin
|
||||
? { destination: 'plugin', plugin: answers.id }
|
||||
: { destination: 'new' };
|
||||
|
||||
return [
|
||||
...baseActions,
|
||||
...plop.getGenerator('content-type').actions({
|
||||
...answers,
|
||||
...destination,
|
||||
}),
|
||||
];
|
||||
},
|
||||
});
|
||||
};
|
||||
|
@ -8,6 +8,7 @@ const ctNamesPrompts = require('./prompts/ct-names-prompts');
|
||||
const kindPrompts = require('./prompts/kind-prompts');
|
||||
const draftAndPublishPrompts = require('./prompts/draft-and-publish-prompts');
|
||||
const getAttributesPrompts = require('./prompts/get-attributes-prompts');
|
||||
const bootstrapApiPrompts = require('./prompts/bootstrap-api-prompts');
|
||||
|
||||
module.exports = plop => {
|
||||
// Model generator
|
||||
@ -19,6 +20,7 @@ module.exports = plop => {
|
||||
...kindPrompts,
|
||||
...getDestinationPrompts('model', plop.getDestBasePath()),
|
||||
...draftAndPublishPrompts,
|
||||
...bootstrapApiPrompts,
|
||||
]);
|
||||
const attributes = await getAttributesPrompts(inquirer);
|
||||
|
||||
@ -45,17 +47,21 @@ module.exports = plop => {
|
||||
|
||||
const filePath = getFilePath(answers.destination);
|
||||
|
||||
return [
|
||||
answers.id = answers.singularName;
|
||||
|
||||
const baseActions = [
|
||||
{
|
||||
type: 'add',
|
||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||
templateFile: 'templates/content-type.schema.json.hbs',
|
||||
data: {
|
||||
id: answers.singularName,
|
||||
collectionName: slugify(answers.pluralName, { separator: '_' }),
|
||||
},
|
||||
},
|
||||
{
|
||||
];
|
||||
|
||||
if (attributes.lenght > 0) {
|
||||
baseActions.push({
|
||||
type: 'modify',
|
||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||
transform(template) {
|
||||
@ -63,8 +69,30 @@ module.exports = plop => {
|
||||
parsedTemplate.attributes = attributes;
|
||||
return JSON.stringify(parsedTemplate, null, 2);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (answers.bootstrapApi) {
|
||||
baseActions.push(
|
||||
{
|
||||
type: 'add',
|
||||
path: `${filePath}/controllers/{{singularName}}.js`,
|
||||
templateFile: 'templates/controller.js.hbs',
|
||||
},
|
||||
];
|
||||
{
|
||||
type: 'add',
|
||||
path: `${filePath}/services/{{singularName}}.js`,
|
||||
templateFile: 'templates/service.js.hbs',
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: `${filePath}/routes/{{singularName}}.js`,
|
||||
templateFile: `templates/${slugify(answers.kind)}-routes.js.hbs`,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return baseActions;
|
||||
},
|
||||
});
|
||||
};
|
||||
|
@ -3,8 +3,8 @@
|
||||
module.exports = [
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'generateDefaultRoutes',
|
||||
name: 'bootstrapApi',
|
||||
default: true,
|
||||
message: 'Generate default routes?',
|
||||
message: 'Bootstrap API related files?',
|
||||
},
|
||||
];
|
Loading…
x
Reference in New Issue
Block a user