diff --git a/examples/getstarted/src/api/address/controllers/address.js b/examples/getstarted/src/api/address/controllers/address.js index 84229885a2..abc4e586d5 100644 --- a/examples/getstarted/src/api/address/controllers/address.js +++ b/examples/getstarted/src/api/address/controllers/address.js @@ -1,5 +1,7 @@ const { createCoreController } = require('@strapi/strapi').factories; +// NOTE: should the file name be useless ? if we use the uid we should generate the controller with the same uid instead ? + module.exports = createCoreController('api::address.address', { async find(ctx) { const { results } = await strapi.service('api::address.address').find(); diff --git a/packages/generators/generators/lib/plops/content-type.js b/packages/generators/generators/lib/plops/content-type.js index 17a56b0bfd..23cf92b1d0 100644 --- a/packages/generators/generators/lib/plops/content-type.js +++ b/packages/generators/generators/lib/plops/content-type.js @@ -3,6 +3,7 @@ const { join } = require('path'); const slugify = require('@sindresorhus/slugify'); const fs = require('fs-extra'); +const { isKebabCase } = require('@strapi/utils'); const getDestinationPrompts = require('./prompts/get-destination-prompts'); const getFilePath = require('./utils/get-file-path'); @@ -33,6 +34,10 @@ module.exports = plop => { default: config.singularName, message: 'Name of the new API?', async validate(input) { + if (!isKebabCase(input)) { + return 'Value must be in kebab-case'; + } + const apiPath = join(plop.getDestBasePath(), 'api'); const exists = await fs.pathExists(apiPath); @@ -101,21 +106,35 @@ module.exports = plop => { } if (answers.bootstrapApi) { + const { singularName } = answers; + + let uid; + if (answers.destination === 'new') { + uid = `api::${answers.id}.${singularName}`; + } else if (answers.api) { + uid = `api::${answers.api}.${singularName}`; + } else if (answers.plugin) { + uid = `plugin::${answers.plugin}.${singularName}`; + } + baseActions.push( { type: 'add', path: `${filePath}/controllers/{{singularName}}.js`, - templateFile: 'templates/controller.js.hbs', + templateFile: 'templates/core-controller.js.hbs', + data: { uid }, }, { type: 'add', path: `${filePath}/services/{{singularName}}.js`, - templateFile: 'templates/service.js.hbs', + templateFile: 'templates/core-service.js.hbs', + data: { uid }, }, { type: 'add', path: `${filePath}/routes/{{singularName}}.js`, - templateFile: `templates/${slugify(answers.kind)}-routes.js.hbs`, + templateFile: `templates/core-router.js.hbs`, + data: { uid }, } ); } diff --git a/packages/generators/generators/lib/templates/core-controller.js.hbs b/packages/generators/generators/lib/templates/core-controller.js.hbs new file mode 100644 index 0000000000..76a8185e90 --- /dev/null +++ b/packages/generators/generators/lib/templates/core-controller.js.hbs @@ -0,0 +1,9 @@ +'use strict'; + +/** + * {{ id }} controller + */ + +const { createCoreController } = require('@strapi/strapi').factories; + +module.exports = createCoreController('{{ uid }}'); diff --git a/packages/generators/generators/lib/templates/core-router.js.hbs b/packages/generators/generators/lib/templates/core-router.js.hbs new file mode 100644 index 0000000000..17c772bb42 --- /dev/null +++ b/packages/generators/generators/lib/templates/core-router.js.hbs @@ -0,0 +1,9 @@ +'use strict'; + +/** + * {{ id }} router. + */ + +const { createCoreRouter } = require('@strapi/strapi').factories; + +module.exports = createCoreRouter('{{ uid }}'); diff --git a/packages/generators/generators/lib/templates/core-service.js.hbs b/packages/generators/generators/lib/templates/core-service.js.hbs new file mode 100644 index 0000000000..9af71a948c --- /dev/null +++ b/packages/generators/generators/lib/templates/core-service.js.hbs @@ -0,0 +1,9 @@ +'use strict'; + +/** + * {{ id }} service. + */ + +const { createCoreService } = require('@strapi/strapi').factories; + +module.exports = createCoreService('{{ uid }}');