rework content-type generator

This commit is contained in:
Dieter Stinglhamber 2021-11-15 14:34:55 +01:00
parent 15d8392f24
commit c32a1921e9
4 changed files with 84 additions and 112 deletions

View File

@ -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 }

View File

@ -3,82 +3,55 @@
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', message: 'API name',
message: 'API name', validate: input => validateInput(input),
validate: input => validateInput(input), },
{
type: 'confirm',
name: 'isPluginApi',
message: 'Is this API for a plugin?',
},
{
when: answers => answers.isPluginApi,
type: 'list',
name: 'plugin',
message: 'Plugin name',
async choices() {
const pluginsPath = join(plop.getDestBasePath(), 'plugins');
const exists = await fs.pathExists(pluginsPath);
if (!exists) {
throw Error('Couldn\'t find a "plugins" directory');
}
const pluginsDir = await fs.readdir(pluginsPath, { withFileTypes: true });
const pluginsDirContent = pluginsDir.filter(fd => fd.isDirectory());
if (pluginsDirContent.length === 0) {
throw Error('The "plugins" directory is empty');
}
return pluginsDirContent;
}, },
{ },
type: 'confirm', {
name: 'isPluginApi', type: 'confirm',
message: 'Is this API for a plugin?', name: 'createContentType',
}, default: true,
{ message: 'Create a content-type?',
when: answers => answers.isPluginApi, },
type: 'list', ],
name: 'plugin',
message: 'Plugin name',
async choices() {
const pluginsPath = join(plop.getDestBasePath(), 'plugins');
const exists = await fs.pathExists(pluginsPath);
if (!exists) {
throw Error('Couldn\'t find a "plugins" directory');
}
const pluginsDir = await fs.readdir(pluginsPath, { withFileTypes: true });
const pluginsDirContent = pluginsDir.filter(fd => fd.isDirectory());
if (pluginsDirContent.length === 0) {
throw Error('The "plugins" directory is empty');
}
return pluginsDirContent;
},
},
{
type: 'confirm',
name: 'createContentType',
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) { 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,41 +70,13 @@ module.exports = plop => {
return baseActions; return baseActions;
} }
if (!answers.createContentType) { return [
return [ {
{
type: 'add',
path: `${filePath}/routes/{{id}}.js`,
templateFile: `templates/single-route.js.hbs`,
},
...baseActions,
];
}
if (answers.generateDefaultRoutes) {
const routeType =
answers.kind === 'singleType'
? 'single-type-routes.js.hbs'
: 'collection-type-routes.js.hbs';
baseActions.push({
type: 'add', type: 'add',
path: `${filePath}/routes/{{id}}.js`, path: `${filePath}/routes/{{id}}.js`,
templateFile: `templates/${routeType}`, templateFile: `templates/single-route.js.hbs`,
}); },
}
const destination =
answers.isPluginApi && answers.plugin
? { destination: 'plugin', plugin: answers.id }
: { destination: 'new' };
return [
...baseActions, ...baseActions,
...plop.getGenerator('content-type').actions({
...answers,
...destination,
}),
]; ];
}, },
}); });

View File

@ -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;
}, },
}); });
}; };

View File

@ -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?',
}, },
]; ];