From c32a1921e9ac48810413d0a41779f8baf1294611 Mon Sep 17 00:00:00 2001 From: Dieter Stinglhamber Date: Mon, 15 Nov 2021 14:34:55 +0100 Subject: [PATCH] rework content-type generator --- .../server/services/content-types.js | 7 +- .../generators/generators/lib/plops/api.js | 147 ++++++------------ .../generators/lib/plops/content-type.js | 38 ++++- ...es-prompts.js => bootstrap-api-prompts.js} | 4 +- 4 files changed, 84 insertions(+), 112 deletions(-) rename packages/generators/generators/lib/plops/prompts/{default-routes-prompts.js => bootstrap-api-prompts.js} (53%) diff --git a/packages/core/content-type-builder/server/services/content-types.js b/packages/core/content-type-builder/server/services/content-types.js index 4ff051b6d3..843464aaba 100644 --- a/packages/core/content-type-builder/server/services/content-types.js +++ b/packages/core/content-type-builder/server/services/content-types.js @@ -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 } diff --git a/packages/generators/generators/lib/plops/api.js b/packages/generators/generators/lib/plops/api.js index 2833db64eb..05344db880 100644 --- a/packages/generators/generators/lib/plops/api.js +++ b/packages/generators/generators/lib/plops/api.js @@ -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, - }), ]; }, }); diff --git a/packages/generators/generators/lib/plops/content-type.js b/packages/generators/generators/lib/plops/content-type.js index 868e85adf1..9710266230 100644 --- a/packages/generators/generators/lib/plops/content-type.js +++ b/packages/generators/generators/lib/plops/content-type.js @@ -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; }, }); }; diff --git a/packages/generators/generators/lib/plops/prompts/default-routes-prompts.js b/packages/generators/generators/lib/plops/prompts/bootstrap-api-prompts.js similarity index 53% rename from packages/generators/generators/lib/plops/prompts/default-routes-prompts.js rename to packages/generators/generators/lib/plops/prompts/bootstrap-api-prompts.js index 568f11e14e..43414939a9 100644 --- a/packages/generators/generators/lib/plops/prompts/default-routes-prompts.js +++ b/packages/generators/generators/lib/plops/prompts/bootstrap-api-prompts.js @@ -3,8 +3,8 @@ module.exports = [ { type: 'confirm', - name: 'generateDefaultRoutes', + name: 'bootstrapApi', default: true, - message: 'Generate default routes?', + message: 'Bootstrap API related files?', }, ];