mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-04 03:43:34 +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,82 +3,55 @@
 | 
			
		||||
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([
 | 
			
		||||
        {
 | 
			
		||||
          type: 'input',
 | 
			
		||||
          name: 'id',
 | 
			
		||||
          message: 'API name',
 | 
			
		||||
          validate: input => validateInput(input),
 | 
			
		||||
    prompts: [
 | 
			
		||||
      {
 | 
			
		||||
        type: 'input',
 | 
			
		||||
        name: 'id',
 | 
			
		||||
        message: 'API name',
 | 
			
		||||
        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',
 | 
			
		||||
          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: '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),
 | 
			
		||||
      };
 | 
			
		||||
    },
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        type: 'confirm',
 | 
			
		||||
        name: 'createContentType',
 | 
			
		||||
        default: true,
 | 
			
		||||
        message: 'Create a content-type?',
 | 
			
		||||
      },
 | 
			
		||||
    ],
 | 
			
		||||
    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,41 +70,13 @@ module.exports = plop => {
 | 
			
		||||
        return baseActions;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (!answers.createContentType) {
 | 
			
		||||
        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({
 | 
			
		||||
      return [
 | 
			
		||||
        {
 | 
			
		||||
          type: 'add',
 | 
			
		||||
          path: `${filePath}/routes/{{id}}.js`,
 | 
			
		||||
          templateFile: `templates/${routeType}`,
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      const destination =
 | 
			
		||||
        answers.isPluginApi && answers.plugin
 | 
			
		||||
          ? { destination: 'plugin', plugin: answers.id }
 | 
			
		||||
          : { destination: 'new' };
 | 
			
		||||
 | 
			
		||||
      return [
 | 
			
		||||
          templateFile: `templates/single-route.js.hbs`,
 | 
			
		||||
        },
 | 
			
		||||
        ...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