185 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-08-18 18:10:24 +02:00
'use strict';
const { join } = require('path');
2021-08-19 16:42:48 +02:00
const fs = require('fs-extra');
2021-08-20 13:26:43 +02:00
const pluralize = require('pluralize');
2021-08-18 18:10:24 +02:00
module.exports = function(plop) {
2021-08-19 16:42:48 +02:00
const rootDir = process.cwd();
plop.setWelcomeMessage('Strapi Generators');
2021-08-20 13:26:43 +02:00
plop.addHelper('pluralize', text => pluralize(text));
2021-08-18 18:10:24 +02:00
// Service generator
plop.setGenerator('service', {
2021-08-19 11:49:55 +02:00
description: 'Generate a service for an API',
2021-08-18 18:10:24 +02:00
prompts: [
{
type: 'input',
name: 'id',
2021-08-19 11:49:55 +02:00
message: 'Service name',
2021-08-18 18:10:24 +02:00
},
],
actions: [
{
type: 'add',
2021-08-19 16:42:48 +02:00
path: join(rootDir, 'api/{{id}}/services/{{id}}.js'),
2021-08-18 18:10:24 +02:00
templateFile: 'templates/service.js.hbs',
},
],
});
2021-08-19 14:15:25 +02:00
// Model generator
plop.setGenerator('model', {
2021-08-19 16:42:48 +02:00
description: 'Generate a model for an API',
2021-08-19 14:15:25 +02:00
prompts: [
{
type: 'input',
name: 'id',
message: 'Model name',
},
{
type: 'confirm',
name: 'useDraftAndPublish',
message: 'Use draft and publish?',
},
],
actions: [
{
type: 'add',
2021-08-19 16:42:48 +02:00
path: join(rootDir, 'api/{{id}}/models/{{id}}.js'),
2021-08-19 14:15:25 +02:00
templateFile: 'templates/model.js.hbs',
},
{
type: 'add',
2021-08-19 16:42:48 +02:00
path: join(rootDir, 'api/{{id}}/models/{{id}}.settings.json'),
2021-08-19 14:15:25 +02:00
templateFile: 'templates/model.settings.json.hbs',
},
],
});
// Controller generator
plop.setGenerator('controller', {
description: 'Generate a controller for an API',
prompts: [
{
type: 'input',
name: 'id',
message: 'Controller name',
},
],
actions: [
{
type: 'add',
2021-08-19 16:42:48 +02:00
path: join(rootDir, 'api/{{id}}/controllers/{{id}}.js'),
templateFile: 'templates/controller.js.hbs',
},
],
});
// Policy generator
plop.setGenerator('policy', {
2021-08-19 16:42:48 +02:00
description: 'Generate a policy for an API',
prompts: [
{
type: 'input',
name: 'id',
message: 'Policy name',
},
],
actions: [
{
type: 'add',
2021-08-19 16:42:48 +02:00
path: join(rootDir, 'config/policies/{{id}}.js'),
templateFile: 'templates/policy.js.hbs',
},
],
});
2021-08-19 16:42:48 +02:00
2021-08-20 13:26:43 +02:00
// API generator
plop.setGenerator('api', {
description: 'Generate a basic API',
prompts: [
{
type: 'input',
name: 'id',
message: 'API name',
},
{
type: 'confirm',
name: 'useDraftAndPublish',
message: 'Use draft and publish?',
},
],
actions: [
{
type: 'add',
path: join(rootDir, 'api/{{id}}/config/routes.json'),
templateFile: 'templates/api-routes.json.hbs',
},
{
type: 'add',
path: join(rootDir, 'api/{{id}}/controllers/{{id}}.js'),
templateFile: 'templates/controller.js.hbs',
},
{
type: 'add',
path: join(rootDir, 'api/{{id}}/models/{{id}}.js'),
templateFile: 'templates/model.js.hbs',
},
{
type: 'add',
path: join(rootDir, 'api/{{id}}/models/{{id}}.settings.json'),
templateFile: 'templates/model.settings.json.hbs',
},
{
type: 'add',
path: join(rootDir, 'api/{{id}}/services/{{id}}.js'),
templateFile: 'templates/service.js.hbs',
},
],
});
2021-08-19 16:42:48 +02:00
// Plugin generator
plop.setGenerator('plugin', {
description: 'Generate a basic plugin',
prompts: [
{
type: 'input',
name: 'id',
message: 'Plugin name',
},
],
actions: data => {
2021-08-20 09:36:40 +02:00
fs.copySync(join(__dirname, 'files', 'plugin'), join(rootDir, 'plugins', data.id));
2021-08-19 16:42:48 +02:00
return [
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/services/{{id}}.js'),
templateFile: 'templates/service.js.hbs',
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/controllers/{{id}}.js'),
templateFile: 'templates/controller.js.hbs',
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/config/routes.json'),
2021-08-20 13:26:43 +02:00
templateFile: 'templates/plugin-routes.json.hbs',
2021-08-19 16:42:48 +02:00
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/README.md'),
templateFile: 'templates/README.md.hbs',
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/package.json'),
templateFile: 'templates/plugin-package.json.hbs',
},
];
},
});
2021-08-18 18:10:24 +02:00
};