Add generators

This commit is contained in:
Alexandre Bodin 2021-11-16 16:53:22 +01:00
parent e928bac64f
commit 08133edd93
5 changed files with 51 additions and 3 deletions

View File

@ -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();

View File

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

View File

@ -0,0 +1,9 @@
'use strict';
/**
* {{ id }} controller
*/
const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('{{ uid }}');

View File

@ -0,0 +1,9 @@
'use strict';
/**
* {{ id }} router.
*/
const { createCoreRouter } = require('@strapi/strapi').factories;
module.exports = createCoreRouter('{{ uid }}');

View File

@ -0,0 +1,9 @@
'use strict';
/**
* {{ id }} service.
*/
const { createCoreService } = require('@strapi/strapi').factories;
module.exports = createCoreService('{{ uid }}');