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 generateAPI = ({ singularName, kind = 'collectionType', pluralName, displayName }) => {
|
||||||
const strapiGenerators = require('@strapi/generators');
|
const strapiGenerators = require('@strapi/generators');
|
||||||
return strapiGenerators.generate(
|
return strapiGenerators.generate(
|
||||||
'api',
|
'content-type',
|
||||||
{
|
{
|
||||||
id: singularName,
|
|
||||||
kind,
|
kind,
|
||||||
singularName,
|
singularName,
|
||||||
pluralName,
|
pluralName,
|
||||||
displayName,
|
displayName,
|
||||||
createContentType: true,
|
destination: 'new',
|
||||||
generateDefaultRoutes: true,
|
bootstrapApi: true,
|
||||||
attributes: [],
|
attributes: [],
|
||||||
},
|
},
|
||||||
{ dir: strapi.dirs.root }
|
{ dir: strapi.dirs.root }
|
||||||
|
@ -3,18 +3,12 @@
|
|||||||
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 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 => {
|
module.exports = plop => {
|
||||||
// API generator
|
// API generator
|
||||||
plop.setGenerator('api', {
|
plop.setGenerator('api', {
|
||||||
description: 'Generate a basic API',
|
description: 'Generate a basic API',
|
||||||
async prompts(inquirer) {
|
prompts: [
|
||||||
const api = await inquirer.prompt([
|
|
||||||
{
|
{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
name: 'id',
|
name: 'id',
|
||||||
@ -55,30 +49,9 @@ module.exports = plop => {
|
|||||||
default: true,
|
default: true,
|
||||||
message: 'Create a content-type?',
|
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) {
|
actions(answers) {
|
||||||
let filePath;
|
const filePath = answers.isPluginApi && answers.plugin ? 'plugins/{{plugin}}' : 'api/{{id}}';
|
||||||
if (answers.isPluginApi && answers.plugin) {
|
|
||||||
filePath = `plugins/{{plugin}}`;
|
|
||||||
} else {
|
|
||||||
filePath = `api/{{id}}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseActions = [
|
const baseActions = [
|
||||||
{
|
{
|
||||||
@ -97,7 +70,6 @@ module.exports = plop => {
|
|||||||
return baseActions;
|
return baseActions;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!answers.createContentType) {
|
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
type: 'add',
|
type: 'add',
|
||||||
@ -106,33 +78,6 @@ module.exports = plop => {
|
|||||||
},
|
},
|
||||||
...baseActions,
|
...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 kindPrompts = require('./prompts/kind-prompts');
|
||||||
const draftAndPublishPrompts = require('./prompts/draft-and-publish-prompts');
|
const draftAndPublishPrompts = require('./prompts/draft-and-publish-prompts');
|
||||||
const getAttributesPrompts = require('./prompts/get-attributes-prompts');
|
const getAttributesPrompts = require('./prompts/get-attributes-prompts');
|
||||||
|
const bootstrapApiPrompts = require('./prompts/bootstrap-api-prompts');
|
||||||
|
|
||||||
module.exports = plop => {
|
module.exports = plop => {
|
||||||
// Model generator
|
// Model generator
|
||||||
@ -19,6 +20,7 @@ module.exports = plop => {
|
|||||||
...kindPrompts,
|
...kindPrompts,
|
||||||
...getDestinationPrompts('model', plop.getDestBasePath()),
|
...getDestinationPrompts('model', plop.getDestBasePath()),
|
||||||
...draftAndPublishPrompts,
|
...draftAndPublishPrompts,
|
||||||
|
...bootstrapApiPrompts,
|
||||||
]);
|
]);
|
||||||
const attributes = await getAttributesPrompts(inquirer);
|
const attributes = await getAttributesPrompts(inquirer);
|
||||||
|
|
||||||
@ -45,17 +47,21 @@ module.exports = plop => {
|
|||||||
|
|
||||||
const filePath = getFilePath(answers.destination);
|
const filePath = getFilePath(answers.destination);
|
||||||
|
|
||||||
return [
|
answers.id = answers.singularName;
|
||||||
|
|
||||||
|
const baseActions = [
|
||||||
{
|
{
|
||||||
type: 'add',
|
type: 'add',
|
||||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||||
templateFile: 'templates/content-type.schema.json.hbs',
|
templateFile: 'templates/content-type.schema.json.hbs',
|
||||||
data: {
|
data: {
|
||||||
id: answers.singularName,
|
|
||||||
collectionName: slugify(answers.pluralName, { separator: '_' }),
|
collectionName: slugify(answers.pluralName, { separator: '_' }),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
];
|
||||||
|
|
||||||
|
if (attributes.lenght > 0) {
|
||||||
|
baseActions.push({
|
||||||
type: 'modify',
|
type: 'modify',
|
||||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||||
transform(template) {
|
transform(template) {
|
||||||
@ -63,8 +69,30 @@ module.exports = plop => {
|
|||||||
parsedTemplate.attributes = attributes;
|
parsedTemplate.attributes = attributes;
|
||||||
return JSON.stringify(parsedTemplate, null, 2);
|
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 = [
|
module.exports = [
|
||||||
{
|
{
|
||||||
type: 'confirm',
|
type: 'confirm',
|
||||||
name: 'generateDefaultRoutes',
|
name: 'bootstrapApi',
|
||||||
default: true,
|
default: true,
|
||||||
message: 'Generate default routes?',
|
message: 'Bootstrap API related files?',
|
||||||
},
|
},
|
||||||
];
|
];
|
Loading…
x
Reference in New Issue
Block a user